Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Apr 11, 2014
1 parent 73396c9 commit abc1bd1
Show file tree
Hide file tree
Showing 22 changed files with 1,445 additions and 65 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change log
### v1.2.0
* Adds Radio, allowing you to create explicit namespaces called Channels. A Channel is made up of
an instance of each of the three messaging systems.

### v1.1.0
* Removes the Error on unhandled commands/requests
* Removes the Error on unhandled commands/requests

### v1.0.1
* update dependencies
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backbone.wreqr",
"version": "1.1.0",
"version": "1.2.0",
"homepage": "https://github.com/marionettejs/backbone.wreqr",
"authors": [
"Derick Bailey"
Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "backbone.wreqr",
"description": "A Simple Service Bus For Backbone and Backbone.Marionette",
"version": "1.1.0",
"version": "1.2.0",
"repo": "marionettejs/backbone.wreqr",
"keywords": [
"backbone",
Expand Down
148 changes: 148 additions & 0 deletions lib/amd/backbone.wreqr.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,154 @@ Wreqr.EventAggregator = (function(Backbone, _){
return EA;
})(Backbone, _);

// Wreqr.Channel
// --------------
//
// An object that wraps the three messaging systems:
// EventAggregator, RequestResponse, Commands
Wreqr.Channel = (function(Wreqr){
"use strict";

var Channel = function(channelName) {
this.vent = new Backbone.Wreqr.EventAggregator();
this.reqres = new Backbone.Wreqr.RequestResponse();
this.commands = new Backbone.Wreqr.Commands();
this.channelName = channelName;
};

_.extend(Channel.prototype, {

// Remove all handlers from the messaging systems of this channel
reset: function() {
this.vent.off();
this.vent.stopListening();
this.reqres.removeAllHandlers();
this.commands.removeAllHandlers();
return this;
},

// Connect a hash of events; one for each messaging system
connectEvents: function(hash, context) {
this._connect('vent', hash, context);
return this;
},

connectCommands: function(hash, context) {
this._connect('commands', hash, context);
return this;
},

connectRequests: function(hash, context) {
this._connect('reqres', hash, context);
return this;
},

// Attach the handlers to a given message system `type`
_connect: function(type, hash, context) {
if (!hash) {
return;
}

context = context || this;
var method = (type === 'vent') ? 'on' : 'setHandler';

_.each(hash, function(fn, eventName) {
this[type][method](eventName, _.bind(fn, context));
}, this);
}
});


return Channel;
})(Wreqr);

// Wreqr.Radio
// --------------
//
// An object that lets you communicate with many channels.
Wreqr.radio = (function(Wreqr){
"use strict";

var Radio = function() {
this._channels = {};
this.vent = {};
this.commands = {};
this.reqres = {};
this._proxyMethods();
};

_.extend(Radio.prototype, {

channel: function(channelName) {
if (!channelName) {
throw new Error('Channel must receive a name');
}

return this._getChannel( channelName );
},

_getChannel: function(channelName) {
var channel = this._channels[channelName];

if(!channel) {
channel = new Wreqr.Channel(channelName);
this._channels[channelName] = channel;
}

return channel;
},

_proxyMethods: function() {
_.each(['vent', 'commands', 'reqres'], function(system) {
_.each( messageSystems[system], function(method) {
this[system][method] = proxyMethod(this, system, method);
}, this);
}, this);
}
});


var messageSystems = {
vent: [
'on',
'off',
'trigger',
'once',
'stopListening',
'listenTo',
'listenToOnce'
],

commands: [
'execute',
'setHandler',
'setHandlers',
'removeHandler',
'removeAllHandlers'
],

reqres: [
'request',
'setHandler',
'setHandlers',
'removeHandler',
'removeAllHandlers'
]
};

var proxyMethod = function(radio, system, method) {
return function(channelName) {
var messageSystem = radio._getChannel(channelName)[system];
var args = Array.prototype.slice.call(arguments, 1);

messageSystem[method].apply(messageSystem, args);
};
};

return new Radio();

})(Wreqr);


return Wreqr;
})(Backbone, Backbone.Marionette, _);
Expand Down
2 changes: 1 addition & 1 deletion lib/amd/backbone.wreqr.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

148 changes: 148 additions & 0 deletions lib/backbone.wreqr.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,154 @@ Wreqr.EventAggregator = (function(Backbone, _){
return EA;
})(Backbone, _);

// Wreqr.Channel
// --------------
//
// An object that wraps the three messaging systems:
// EventAggregator, RequestResponse, Commands
Wreqr.Channel = (function(Wreqr){
"use strict";

var Channel = function(channelName) {
this.vent = new Backbone.Wreqr.EventAggregator();
this.reqres = new Backbone.Wreqr.RequestResponse();
this.commands = new Backbone.Wreqr.Commands();
this.channelName = channelName;
};

_.extend(Channel.prototype, {

// Remove all handlers from the messaging systems of this channel
reset: function() {
this.vent.off();
this.vent.stopListening();
this.reqres.removeAllHandlers();
this.commands.removeAllHandlers();
return this;
},

// Connect a hash of events; one for each messaging system
connectEvents: function(hash, context) {
this._connect('vent', hash, context);
return this;
},

connectCommands: function(hash, context) {
this._connect('commands', hash, context);
return this;
},

connectRequests: function(hash, context) {
this._connect('reqres', hash, context);
return this;
},

// Attach the handlers to a given message system `type`
_connect: function(type, hash, context) {
if (!hash) {
return;
}

context = context || this;
var method = (type === 'vent') ? 'on' : 'setHandler';

_.each(hash, function(fn, eventName) {
this[type][method](eventName, _.bind(fn, context));
}, this);
}
});


return Channel;
})(Wreqr);

// Wreqr.Radio
// --------------
//
// An object that lets you communicate with many channels.
Wreqr.radio = (function(Wreqr){
"use strict";

var Radio = function() {
this._channels = {};
this.vent = {};
this.commands = {};
this.reqres = {};
this._proxyMethods();
};

_.extend(Radio.prototype, {

channel: function(channelName) {
if (!channelName) {
throw new Error('Channel must receive a name');
}

return this._getChannel( channelName );
},

_getChannel: function(channelName) {
var channel = this._channels[channelName];

if(!channel) {
channel = new Wreqr.Channel(channelName);
this._channels[channelName] = channel;
}

return channel;
},

_proxyMethods: function() {
_.each(['vent', 'commands', 'reqres'], function(system) {
_.each( messageSystems[system], function(method) {
this[system][method] = proxyMethod(this, system, method);
}, this);
}, this);
}
});


var messageSystems = {
vent: [
'on',
'off',
'trigger',
'once',
'stopListening',
'listenTo',
'listenToOnce'
],

commands: [
'execute',
'setHandler',
'setHandlers',
'removeHandler',
'removeAllHandlers'
],

reqres: [
'request',
'setHandler',
'setHandlers',
'removeHandler',
'removeAllHandlers'
]
};

var proxyMethod = function(radio, system, method) {
return function(channelName) {
var messageSystem = radio._getChannel(channelName)[system];
var args = Array.prototype.slice.call(arguments, 1);

messageSystem[method].apply(messageSystem, args);
};
};

return new Radio();

})(Wreqr);


return Wreqr;
})(Backbone, Backbone.Marionette, _);
Loading

0 comments on commit abc1bd1

Please sign in to comment.