javascript - Backbone.js model delete failure - Stack Overflow

So right now when I destroy a model and the server returns an error, the destroy event gets fired anywa

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745507497a4630617.html

相关推荐

  • javascript - Backbone.js model delete failure - Stack Overflow

    So right now when I destroy a model and the server returns an error, the destroy event gets fired anywa

    23小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信