Is Backbone js and Marionette js relevant

Marionette Events

The Marionette Event system provides a system for objects to communicate with each other in a uniform way. In Marionette, this involves one object triggering an event that another listens to. This is an extended from of the event handling system in Backbone, and is different than DOM related events. It is mixed in to every Marionette class.

Documentation Index

Triggering and Listening to Events

The traditional event handling system in Backbone is fully supported in Marionette. Marionette, however, provides an additional event API using the method - the key difference between the two is that automatically calls specially named event handlers.

Just like 's the method fires the named event on the instance - any listeners will then be triggered on the event. If there are no listeners, this call will still succeed. All arguments after the first event name string will be passed to all event handlers.

Live example

The method is available to all Marionette classes.

Listening to Events

Marionette's event triggers work just like regular Backbone events - you can use and to act on events:

Live example

You can also use as in Backbone:

Live examples

As in Backbone, will pass the object it is called on in as the context variable. These behave exactly as in Backbone, so using will require you to unhook any event handlers yourself to prevent memory leaks. Marionette, however, does provide extra helpers as part of the view lifecycle that bind and unbind event handlers for you. this is the core of Binding.

Binding

The major difference between and is that can fire specially named events on the instance. For example, a view that has been rendered will iternally fire and call - providing a handy way to add behavior to your views.

Determining what method an event will call is easy, we will outline this with an example using though this also works with any custom events you want to fire:

  1. Split the words around the characters - so , ,
  2. Capitalize the first letter of each word - , ,
  3. Add a leading - , , ,
  4. Mash it into a single call -

Using this process, will call the method. Let's see it in action with a custom event:

Live example

As before, all arguments passed into after the event name will make their way into the event handler. Using this method ensures there will be no unexpected memory leaks.

View and

Views can automatically bind DOM events to methods and View events with and respectively:

Live example

For more information, see the DOM interactions documentation.

View entity events

Views can automatically bind to its model or collection with and respectively.

Live example

For more information, see the Entity events documentation.

Child View Events

The and are able to monitor and act on events on any of their direct children. Any events fired on a view are automatically propagated to their direct parents as well. Let's see a quick example:

Live example

Event Bubbling

Events fired on a view bubble up to their direct parent views, calling any event methods using the prefix (more on that shortly) and any methods bound to the attribute. This works for built-in events, custom events fired with and bound events using .

NOTE Automatic event bubbling can be disabled by setting to .

When using implicit listeners, the event prefix is used which needs to be included as part of the handler:

NOTE will automatically pass the child view as an argument to the parent view, however will not, and so notice that in the above example, the explicitly passes the child view.

Live example

Using

This works exactly the same way for the and its :

Live examples

A Child View's Event Prefix

You can customize the event prefix for events that are forwarded through the view. To do this, set the on the view or collectionview. For more information on the see Event bubbling.

The default value for is . Setting this property to will disable automatic event bubbling.

Live example

The can be provided in the view definition or in the constructor function call, to get a view instance.

Explicit Event Listeners

To call specific functions on event triggers, use the attribute to map child events to methods on the parent view. This takes events fired on child views - without the prefix - and calls the method referenced or attached function.

Live example

Attaching Functions

The attribute can also attach functions directly to be event handlers:

Live example

Using 's

Live example

Triggering Events on Child Events

A hash or method permits proxying of child view events without manually setting bindings. The values of the hash should be a string of the event to trigger on the parent.

is sugar on top of much in the same way that view are sugar for view .

Live example

Using 's

Live example

Lifecycle Events

Marionette Views fire events during their creation and destruction lifecycle. For more information see the documentation covering the Lifecycle.