Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroying all models from a collection #139

Closed
luniki opened this issue Dec 14, 2010 · 4 comments
Closed

Destroying all models from a collection #139

luniki opened this issue Dec 14, 2010 · 4 comments
Labels

Comments

@luniki
Copy link

luniki commented Dec 14, 2010

Not so obviously 'collection.invoke("destroy")' does not work. How would one empty a collection?

@martindrapeau
Copy link

I ended up extending both Model and Collection with methods for those. Here those are:
MyModel = Backbone.Model.extend({
destroy : function(options) {
options || (options = {});
if (this.selected) this.unselect(options);
if (!options.silent) this.trigger('destroy', this, options);
if (this.collection) this.collection.remove(this, options);
return this;
}
});
MyCollection = Backbone.Collection.extend({
clear: function(options) {
options || (options = {});
while (this.length > 0) {
this.at(0).destroy(options);
}
if (!options.silent) this.trigger('clear', this, options);
return this;
}
});

@jashkenas
Copy link
Owner

So, there are two things here. The first is destroying all the models in a collection on both the client and the server, and the second is simply removing all the models from a collection, client-side.

The reason why backbone doesn't have the former built-in is because it's not standard in basic REST, and we won't want to encourage folks to be submitting 100 individual HTTP requests, one to destroy each item. You should have a special endpoint to perform a bulk destroy, and then empty your collection client-side, which can be done like so:

collection.refresh();

@martindrapeau
Copy link

What happens if you have code that watches a particular model? It would never know it got removed.
Would be nice to have the event triggered but the REST call not made. Hence in my implementation the destroy/clear are not REST (no calls to server). I have specialized methods delete/deleteAll to do that. But each model does trigger the destroy event so watchers get notified. I'll admit I don't like my current implementation because it is not clean. So if server calls could be made optional, and events always triggered, I'd be happy.

@jwogan5
Copy link

jwogan5 commented Dec 12, 2019

@martindrapeau

I have been searching for a long time but I came across this and was wondering if you could send me your deleteAll method for a backbone collection. Here is my issue. I hit an api to get a list of items and I store them in a collection. I then want to completely remove all items in the collection and refresh them with a nw list. I can do that locally but when I call fetch on the collection all the items come back again as they are not removed from the server. I can also do the whole removing one item at a time from the server but that can take 15 seconds to finish if I have about 200 items. Preferably I want to both call refresh locally and one call to remove all items on the server to speed up the removal process. Any help would be appreciated.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants