最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone.js model delete failure - Stack Overflow

programmeradmin7浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 8

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/#Model-destroy

发布评论

评论列表(0)

  1. 暂无评论