From fc6175dbb75d822ed05f1b3ccda72580aba30164 Mon Sep 17 00:00:00 2001 From: Jmeas Date: Sun, 26 Oct 2014 00:09:39 -0400 Subject: [PATCH] listenTo equivalents for Commands and Requests. --- src/backbone.radio.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/backbone.radio.js b/src/backbone.radio.js index 60c419e..8880b11 100644 --- a/src/backbone.radio.js +++ b/src/backbone.radio.js @@ -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. @@ -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.