Trunk events when simulating assembly firing (double firing)

The Backbone application I am developing has a collection and model, as well as related views for each element.

https://gist.github.com/2255959

When I click on PostView, the event unexpectedly fires in the collection without any wiring.

I figured that I needed to associate an event with a model, and then fire this fire in a collection. Is that not so? Does the collection collect automatically inherited events from its child models?

I'm not sure, but I think this has something to do with nested views, and maybe the event is bound to both places, not just the inner el .

+6
source share
1 answer

From the exact guide :

Any event that is fired on the model in the collection will also be fired directly to the collection, for convenience.

So, yes, the collection listens for events on all of its models and forwards them.

For example, with a simple setup:

 class M extends Backbone.Model class C extends Backbone.Collection model: M c = new C c.on('change', (model, opts) -> console.log('Change on collection')) 

Running c.first().set(...) will call the event handler.

Demo: http://jsfiddle.net/ambiguous/wwjnK/

+14
source

Source: https://habr.com/ru/post/912089/


All Articles