Cheenu Madan
Cheenu Madan

Reputation: 129

Backbone - How to prevent models from being added to and removed from collections on server side validation failures

For example, if I do a destroy on a model and the server returns a 4xx/5xx, the error callback handler is called but the model is removed from the collection anyway. How I do prevent this from happening?

Upvotes: 1

Views: 1707

Answers (1)

Derick Bailey
Derick Bailey

Reputation: 72868

Backbone is optimistic about this starting in v0.9. From the docs at http://backbonejs.org/#upgrading

Creating and destroying models is now optimistic. Pass {wait: true} if you need the previous behavior of waiting for the server to acknowledge success. You can now also pass {wait: true} to save calls.

So all you have to do is set the {wait: true} flag in your save:

myModel.save(null, {wait: true});

or in your delete:

myModel.destroy({wait: true})

Upvotes: 8

Related Questions