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 completes, I am wondering if there is any way to stop it.
Thanks
To 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With