Skip to content

Instantly share code, notes, and snippets.

@fgrehm

fgrehm/gist:4127812

Created Nov 21, 2012
Embed
What would you like to do?
Backbone.Events.once
// 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
You can’t perform that action at this time.