模型发射的骨干事件(双重射击) [英] Backbone events on model firing on collection (Double firing)
问题描述
我正在开发的骨干应用程序有一个集合和一个模型,并且每个项目都有相关的视图。
A Backbone app which I'm developing has a collection and a model, and associated views for each item.
https://gist.github.com/2255959
当我点击PostView时,意外的是,事件在集合上触发而没有任何布线。
When I click on the PostView, unexpectedly, the event fires on the collection without any wiring.
我想我需要绑定一个事件到模型,然后在该集合上触发一个事件。不是吗?一个集合是否自动继承事件触发了其子模型?
I figured I'd need to bind an event to the model, then have that fire an event on the collection. Is that not the case? Does a collection automagically inherit events fired its child models?
我不确定,但我认为这与嵌套视图有关,也许事件正在绑定在两个地方,而不是内部 el
。
I'm uncertain, but I think it has something to do with the nested views, and maybe the event is being bound on both places instead of just the inner el
.
推荐答案
从精细手册:
为了方便,在集合中的模型上触发的任何事件也将被直接触发。
Any event that is triggered on a model in a collection will also be triggered on the collection directly, for convenience.
所以是该集合侦听所有模型上的事件并转发它们。
So yes, the collection listens to events on all of its models and forwards them.
例如,给出了一个简单的设置:
For example, given a simple set up like this:
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'))
执行 c.first()。set(...)
将触发事件处理程序。
Doing c.first().set(...)
will trigger the event handler.
演示: http://jsfiddle.net/ambiguous/wwjnK/
这篇关于模型发射的骨干事件(双重射击)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!