Skip to content

Commit fc6175d

Browse files
committed
listenTo equivalents for Commands and Requests.
1 parent 1ceb392 commit fc6175d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/backbone.radio.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ Radio.Commands = {
233233
}
234234
};
235235

236+
// listenTo equivalent for Commands
237+
var listenMethods = {complyFor: 'comply', complyForOnce: 'complyOnce'};
238+
_.each(listenMethods, function(implementation, method) {
239+
Radio.Commands[method] = function(obj, name, callback) {
240+
var listeningTo = this._listeningTo || (this._listeningTo = {});
241+
var id = obj._listenId || (obj._listenId = _.uniqueId('c'));
242+
listeningTo[id] = obj;
243+
if (!callback && typeof name === 'object') { callback = this; }
244+
obj[implementation](name, callback, this);
245+
return this;
246+
};
247+
});
248+
236249
//
237250
// Backbone.Radio.Requests
238251
// A messaging system for requesting data.
@@ -322,6 +335,19 @@ Radio.Requests = {
322335
}
323336
};
324337

338+
// listenTo equivalent for Requests
339+
var listenMethods = {replyFor: 'reply', replyForOnce: 'replyOnce'};
340+
_.each(listenMethods, function(implementation, method) {
341+
Radio.Requests[method] = function(obj, name, callback) {
342+
var listeningTo = this._listeningTo || (this._listeningTo = {});
343+
var id = obj._listenId || (obj._listenId = _.uniqueId('r'));
344+
listeningTo[id] = obj;
345+
if (!callback && typeof name === 'object') { callback = this; }
346+
obj[implementation](name, callback, this);
347+
return this;
348+
};
349+
});
350+
325351
//
326352
// Backbone.Radio.channel
327353
// Get a reference to a channel by name.

0 commit comments

Comments
 (0)