Backbone model.destroy(): Is explicit removal from collection necessary?

AndraD picture AndraD · Jan 14, 2013 · Viewed 34.5k times · Source

I have a simple question. I am looking at a function with 2 lines of code:

deleteTask: function() {
    this.parent.collection.remove(this.model);
    this.model.destroy();
}

If I comment out the first line, which is supposed to remove the model from its collection, things seem to work as intended (as in, the model is removed automatically). From Backbone's website, this is the relevant discription for a model's "destroy" function:

Triggers a "destroy" event on the model, which will bubble up through any collections that contain it.

Am I safe to assume that the removal of this.parent.collection.remove(this.model); will not affect the functionality of the code in any way? This is what I think, but I wanted to make sure of it.

Thank you!

Answer

asgeo1 picture asgeo1 · Jan 14, 2013

If you destroy a model, it is removed from any collections that was containing it. You can see that in the backbone source

//Internal method called every time a model in the set fires an event.
_onModelEvent: function(event, model, collection, options) {
    ...
    if (event === 'destroy') this.remove(model, options);

So yes, I wouldn't think you would need to remove the model from your collection explicitly.

But don't trust me, test for yourself :)

deleteTask: function() {
    that = this;
    this.model.destroy({
      success: function() {
        console.log(that.parent.collection);
      }
    });
}

Check the console for yourself to see whether the model was removed from the collection.