So right now when I destroy a model and the server returns an error, the destroy event gets fired anyway and the model data gets reset..
Is there anyway to prevent that from happening?
You can pass {wait: true}
as an option to destroy()
, which causes it to wait for a response from the server before removing the model from the collection.
In terms of binding views to events, you should be handling the remove
and destroy
events separately, as the remove
event will be fired when the server returns successfully, but never if the server returns an error.
Alternatively you could pass an error
handler to model.destroy
, which you can then use to put the model back if the server fails for any reason. Either simply add the model back into your collection (if you have one), or cause the data to re-load from the server (which may be the safer option).
Note that with this method, the remove
event will still fire, and be followed by an add
event when you put the model back.
thing.destroy({error: function(model, response) {
// put the thing back in the collection, or cause the collection to reload
}});
You mentioned the model data being reset; I believe the model
argument in the error handler above receives the original model, so you can still access its data.
For more information see the backbone docs - http://backbonejs.org/#Model-destroy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With