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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can pass
{wait: true}as an option todestroy(), 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
removeanddestroyevents separately, as theremoveevent will be fired when the server returns successfully, but never if the server returns an error.Alternatively you could pass an
errorhandler tomodel.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
removeevent will still fire, and be followed by anaddevent when you put the model back.You mentioned the model data being reset; I believe the
modelargument 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