Skip to content

Commit

Permalink
listenTo equivalents for Commands and Requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Oct 26, 2014
1 parent 1ceb392 commit fc6175d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/backbone.radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,19 @@ Radio.Commands = {
}
};

// listenTo equivalent for Commands
var listenMethods = {complyFor: 'comply', complyForOnce: 'complyOnce'};
_.each(listenMethods, function(implementation, method) {
Radio.Commands[method] = function(obj, name, callback) {
var listeningTo = this._listeningTo || (this._listeningTo = {});
var id = obj._listenId || (obj._listenId = _.uniqueId('c'));
listeningTo[id] = obj;
if (!callback && typeof name === 'object') { callback = this; }
obj[implementation](name, callback, this);
return this;
};
});

//
// Backbone.Radio.Requests
// A messaging system for requesting data.
Expand Down Expand Up @@ -322,6 +335,19 @@ Radio.Requests = {
}
};

// listenTo equivalent for Requests
var listenMethods = {replyFor: 'reply', replyForOnce: 'replyOnce'};
_.each(listenMethods, function(implementation, method) {
Radio.Requests[method] = function(obj, name, callback) {
var listeningTo = this._listeningTo || (this._listeningTo = {});
var id = obj._listenId || (obj._listenId = _.uniqueId('r'));
listeningTo[id] = obj;
if (!callback && typeof name === 'object') { callback = this; }
obj[implementation](name, callback, this);
return this;
};
});

//
// Backbone.Radio.channel
// Get a reference to a channel by name.
Expand Down

0 comments on commit fc6175d

Please sign in to comment.