Ember数据删除失败,如何回滚 [英] Ember Data delete fails, how to rollback
问题描述
如果我调用destroyRecord并且在服务器上失败,它也会从本地存储和UI消失。如果删除失败,我需要以某种方式回滚。我试过这样的东西。
If I call destroyRecord and it fails on the server, it also disappears from the local store and from the UI. I need to somehow "rollback" if delete fails. I have tried something like this.
item.destroyRecord().then(function () {
Notify.success("item removed");
}).catch(function (response) {
//NEED TO ROLLBACK HERE - ANY IDEAS?
Notify.error('Failed to remove!');
});
推荐答案
首先,回滚与关系不能完全奏效ember数据,其次更新版本的ember数据处理更好(ember数据1.0 beta 7+)。记录在这个目的上有一个回滚方法,它仍然是beta,但它主要是你要找的。 p>
Firstly, rollback with relationships doesn't fully work in ember data, secondly the newer versions of ember data handle this better (ember data 1.0 beta 7+). Records have a rollback method on them for this very purpose, it's still in beta, but it does mostly what you're looking for.
item.destroyRecord().then(function () {
Notify.success("item removed");
}).catch(function (response) {
item.rollback();
Notify.error('Failed to remove!');
});
注意:在较新版本的Ember中, item.rollback()
不再使用功能,而是使用Marcelo的评论中提到的 item.rollbackAttributes()
。
NOTE: In newer versions of Ember, item.rollback()
no longer functions, instead use item.rollbackAttributes()
as mentioned in comments from Marcelo.
这篇关于Ember数据删除失败,如何回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!