Ember Data delete fails, how to rollback

不问归期 提交于 2020-01-14 08:35:47

问题


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!');
        });

回答1:


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!');
    });

NOTE: In newer versions of Ember, item.rollback() no longer functions, instead use item.rollbackAttributes() as mentioned in comments from Marcelo.



来源:https://stackoverflow.com/questions/23937976/ember-data-delete-fails-how-to-rollback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!