Backbone JS: how to disable sync for delete?

mvbl fst picture mvbl fst · Apr 18, 2012 · Viewed 9.9k times · Source

I am dealing with a threaded comments collection, and when I delete a comment that has children, I do model.destroy() for this comment, and on the server side all its branches get deleted.

I wrote a function that once a node is deleted from the tree, looks for all orphans and removes them too. So when I find orphans, I run model.destroy() on them too but because they're already deleted on the server, sync returns errors.

Is there a way to disable sync for some destroy() calls?

Answer

Edward M Smith picture Edward M Smith · Apr 19, 2012

Since all the destroy method does is send a DELETE request and triggers destroy, simply triggering destroy is exactly what you're looking for.

model.trigger('destroy', model, model.collection, options);

Yeah, it feels a bit hackish, but that's pretty much all the Backbone code does anyway. If you want, and if you have a base model you extend from, you could add this as a method on that base model, and it might not feel quite so hackish.