Skip to content

Commit

Permalink
Add Events#oneTimeListenTo
Browse files Browse the repository at this point in the history
  • Loading branch information
gsamokovarov committed Jan 17, 2013
1 parent 87ee87d commit 02d4234
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@
}
};

var listenMethods = {listenTo: 'on', listenToOnce: 'once'};

// An inversion-of-control versions of `on` and `once`. Tell *this* object to listen to
// an event in another object ... keeping track of what it's listening to.
_.each(listenMethods, function(eventBinder, method) {
Events[method] = function(object, events, callback) {
var listeners = this._listeners || (this._listeners = {});
var id = obj._listenerId || (obj._listenerId = _.uniqueId('l'));
listeners[id] = obj;
obj[eventBinder](name, typeof name === 'object' ? this : callback, this);
return this;
};
});

// Aliases for backwards compatibility.
Events.bind = Events.on;
Events.unbind = Events.off;
Expand Down

0 comments on commit 02d4234

Please sign in to comment.