Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
:x: [Deprecated] Manage Backbone Events Better
JavaScript
tree: a5cbd69525

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
public/javascripts
spec/javascripts
src
.gitignore
CHANGELOG.md
LICENSE.md
grunt.js
package.json
readme.md

readme.md

Backbone.EventBinder

Manage your Backbone events better.

About Backbone.EventBinder

Backbone's events are a great way to decouple parts of your system, but they have some limitations and behaviors that you need to be aware of which can cause zombie objects and memory leaks if you're not careful.

Backbone.EventBinder provides a simple mechanism for cleaning up your event bindings, including the ability to clean up anonymous callback functions!

Downloads And Source

Grab the source from the src folder above. Grab the most recent builds from the links below.

Standard Builds

RequireJS (AMD) Builds

Documentation

The EventBinder object provides event binding management for related events, across any number of objects that trigger the events. This allows events to be grouped together and unbound with a single call during the clean-up of an object that is bound to the events.

Bind Events

var binder = new Backbone.EventBinder();

var model = new MyModel();

var handler = {
  doIt: function(){}
}

binder.bindTo(model, "change:foo", handler.doIt);

You can optionally specify a 4th parameter as the context in which the callback method for the event will be executed:

binder.bindTo(model, "change:foo", someCallback, someContext);

Unbind A Single Event

When you call bindTo, it returns a "binding" object that can be used to unbind from a single event with the unbindFrom method:

var binding = binder.bindTo(model, "change:foo", someCallback, someContext);

// later in the code
binder.unbindFrom(binding);

This will unbind the event that was configured with the binding object, and remove it from the EventBinder bindings.

Unbind All Events

You can call unbindAll to unbind all events that were bound with the bindTo method:

binder.unbindAll();

This even works with in-line callback functions.

When To Use EventBinder vs on Handlers

See the wiki: When to use the EventBinder

License

MIT - see LICENSE.md

Something went wrong with that request. Please try again.