A simple infrastructure based on messaging patterns and service bus implementations for decoupling Backbone and Backbone.Marionette applications.
Grab the source from the src
folder above. Grab the most recent builds
from the links below.
-
Development: backbone.wreqr.js
-
Production: backbone.wreqr.min.js
-
Development: backbone.wreqr.js
-
Production: backbone.wreqr.min.js
An event aggregator implementation. It extends from Backbone.Events
to
provide the core event handling code in an object that can itself be
extended and instantiated as needed.
var vent = new Backbone.Wreqr.EventAggregator();
vent.on("foo", function(){
console.log("foo event");
});
vent.trigger("foo");
Wreqr can be used by instantiating a Backbone.Wreqr.Commands
or Backbone.Wreqr.RequestResponse
object. These objects provide an
addHandler
method to add a handler for a named request or command.
Commands can then be executed with the execute
method, and
request/response can be done through the request
method.
var commands = new Backbone.Wreqr.Commands();
commands.addHandler("foo", function(){
console.log("the foo command was executed");
});
commands.execute("foo");
var reqres = new Backbone.Wreqr.RequestResponse();
reqres.addHandler("foo", function(){
return "foo requested. this is the response";
});
var result = reqres.request("foo");
console.log(result);
Removing handlers for commands or requests is done the
same way, with the removeHandler
or removeAllHandlers
functions.
reqres.removeHandler("foo");
commands.removeAllHandlers();
The EventAggregator, Commands and RequestResponse objects can all be
extended using Backbone's standard extend
method.
var MyEventAgg = Backbone.Wreqr.EventAggregator.extend({
foo: function(){...}
});
var MyCommands = Backbone.Wreqr.Commands.extend({
foo: function(){...}
});
var MyReqRes = Backbone.Wreqr.RequestResponse.extend({
foo: function(){...}
});
MIT - see LICENSE.md