I’m curious to find out why resetting a backbone collection doesn’t fire a model event. However, it seems only logical to fire a model event when a model is physically being removed from a collection.
Is this intentional or am I missing something? If backbone doesn’t do this sort of thing what’s a good practice for delegating events like so.
Why does backbone not trigger a model event when its collection resets?
var TicketModel = Backbone.Model.extend({
defaults: {
name: 'crafty',
email: 'dwq@dwqcqw.com'
},
initialize: function(){
this.on("all", function(event){
console.log(event)
});
}
});
var TicketCollection = Backbone.Collection.extend({
model: TicketModel,
});
var tickets = new TicketCollection([
{
name: 'halldwq'
},
{
name: 'dascwq'
},
{
name: 'dsacwqe'
}
]);
tickets.reset();
Overriding Backbone method can cause pain when updating to another version.
Backbone stores an array of the models before the reset in options.previousModels, so just listen to the reset event and trigger a ‘remove’ event on those previous models:
That’d do the trick.