admin管理员组文章数量:1332881
How do I prevent Backbone Model events from propagating to Backbone Collections?
Edit:
Let's say I have something like the following, where CollectionView contains a collection of MyModels...
var CollectionView = Backbone.Collection.Extend({
initialize: function() {
this.collection.on("change", doStuff);
}
});
var ModelView = Backbone.View.Extend({
initialize: function() {
this.model = new MyModel();
this.model.on( "change", doStuff );
this.model.fetch();
}
});
If in a special case I did not want the "change" event to propagate up to the collection after fetch pletes, I am wondering if there is any way to stop it.
Thanks
How do I prevent Backbone Model events from propagating to Backbone Collections?
Edit:
Let's say I have something like the following, where CollectionView contains a collection of MyModels...
var CollectionView = Backbone.Collection.Extend({
initialize: function() {
this.collection.on("change", doStuff);
}
});
var ModelView = Backbone.View.Extend({
initialize: function() {
this.model = new MyModel();
this.model.on( "change", doStuff );
this.model.fetch();
}
});
If in a special case I did not want the "change" event to propagate up to the collection after fetch pletes, I am wondering if there is any way to stop it.
Thanks
Share Improve this question edited Feb 15, 2013 at 19:00 user1031947 asked Feb 15, 2013 at 18:36 user1031947user1031947 6,67417 gold badges65 silver badges91 bronze badges 3- 1 Models don't fire the reset event, could you expand on your question? Specifically what code are you running that is firing the event you don't want to propagate? – Tomdarkness Commented Feb 15, 2013 at 18:42
- To @Tomdarkness's point, the code you've shown is the event binding. What is actually triggering the event? (I think this is a good question, because the answer is not apparent in the docs AFAIK.) – Evan Davis Commented Feb 15, 2013 at 18:47
- I wasn't aware that models did not fire the reset event -- good to know. – user1031947 Commented Feb 15, 2013 at 19:01
1 Answer
Reset to default 8To prevent a model from firing a change event:
model.set(attrs, {silent: true});
This may not be what you want, though, because this will also prevent the model's change event from firing.
Collections pass through all model events, but what you can do is pass extra options which will also get passed through:
model.set(attrs, {dontBubble: true});
And in your CollectionView:
var CollectionView = Backbone.View.extend({
this.initialize = function() {
this.collection.on('change', doStuff, this);
},
this.doStuff = function(model, collection, options) {
if (options.dontBubble) {
return;
}
// Do some stuff.
}
});
Granted, it's a little ugly, but it's one way to go about it.
本文标签: javascriptHow to prevent backbone model to collection event propagationStack Overflow
版权声明:本文标题:javascript - How to prevent backbone model to collection event propagation? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742347904a2457897.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论