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.

Documentation Index

Bind Events

```js var binder = new Backbone.Marionette.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:

js 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:

```js 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:

js 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

Project Versions

Table Of Contents

Previous topic


Next topic

Marionette functions

This Page