admin管理员组文章数量:1427829
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?
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?
Share Improve this question asked Nov 27, 2012 at 21:36 PwnnaPwnna 9,56821 gold badges67 silver badges94 bronze badges1 Answer
Reset to default 8You 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/#Model-destroy
本文标签: javascriptBackbonejs model delete failureStack Overflow
版权声明:本文标题:javascript - Backbone.js model delete failure - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745507496a2661312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论