Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroying a Backbone Model in a Collection in one step?

Tags:

Are these two steps mandatory to delete a Model?

var model = collection.get(id);
model.destroy();
collection.remove(model);

Isn't there a way to destroy a model when it is removed from its collection?

like image 710
yves amsellem Avatar asked Jun 08 '11 14:06

yves amsellem


1 Answers

Model.bind("remove", function() {
  this.destroy();
});
...
var model = new Model();
...
collection.remove(model);

Removing a model from a collection triggers the "remove" event.

So if you want to, you can get models to bind to them and destroy themselves.

like image 104
Raynos Avatar answered Oct 26 '22 18:10

Raynos