Created
November 21, 2012 21:20
-
-
Save fgrehm/4127812 to your computer and use it in GitHub Desktop.
Backbone.Events.once
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on | |
// https://github.com/tbranyen/backbone/blob/70eed5bc9e5d3587bd85e9ff56f8ea99a7f8501d/backbone.js#L130-141 | |
// Bind an event like `on`, but unbind the event following the first trigger. | |
window.Backbone.Model.prototype.once = | |
window.Backbone.View.prototype.once = | |
window.Backbone.Events.once = function (events, callback, context) { | |
// Bind the original events. | |
this.on(events, callback, context); | |
// Bind a new event immediately preceeding the original to unbind the original once called. | |
this.on(events, function unbind() { | |
// Remove the original event and the cleanup event. | |
this.off(events, callback, context).off(events, unbind); | |
}, this); | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment