Skip to content

Commit

Permalink
Register stopListening once instead of wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell authored and akre54 committed Nov 3, 2014
1 parent 7bf1f2a commit 3461128
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,24 +241,18 @@
};

Events.listenToOnce = function(obj, name, callback) {
var ctx = this;
var wrapCb = function(event, cb) {
var wrapped = function() {
ctx.stopListening(obj, event, wrapped);
return cb.apply(ctx, arguments);
};
return wrapped;
}
this.listenTo(obj, name, callback);

if (typeof name === 'object') {
for (var event in name) {
name[event] = wrapCb(event, name[event]);
}
} else {
callback = wrapCb(name, callback);
var onces = name;
if (typeof onces !== 'object') {
onces = {};
onces[name] = callback;
}
return this.listenTo(obj, name, callback);
}
for (var event in onces) {
obj.once(event, _.bind(this.stopListening, this, obj, event, onces[event]), this);
}
return this;
};

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

0 comments on commit 3461128

Please sign in to comment.