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.
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.
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:
You can also use as in Backbone:
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.
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:
- Split the words around the characters - so , ,
- Capitalize the first letter of each word - , ,
- Add a leading - , , ,
- Mash it into a single call -
Using this process, will call the method. Let's see it in action with a custom event:
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.
Views can automatically bind DOM events to methods and View events with and respectively:
For more information, see the DOM interactions documentation.
View entity events
Views can automatically bind to its model or collection with and respectively.
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:
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.
This works exactly the same way for the and its :
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.
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.
The attribute can also attach functions directly to be event handlers:
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 .
Marionette Views fire events during their creation and destruction lifecycle. For more information see the documentation covering the Lifecycle.
- Business Cycles What is a downturn
- Are brilliant minds born or made
- Where do I buy shoelaces in Singapore
- Supervillains Who is Flashs most dangerous adversary
- What percent of Christians are Evangelicals
- Is racial discrimination prevalent in South America
- What are best full frame camera
- How do you fix an electrical outlet
- Should a medical aspirant quit social media
- How does immigration affect society
- Can reading help dyslexia?no_redirect=1
- Which treatment is best for hairfall
- How does TVF make money
- Why is Hindi less valuable than English
- Can you use chicken for fishing bait
- How are atheists treated in Cameroon
- Could nanotechnology be weaponized
- Does Head Shoulders shampoo damage the hair