Ember数据删除失败,如何回滚

Ema*_*mad 6 ember.js ember-data

如果我调用destroyRecord并且它在服务器上失败,它也会从本地存储和UI中消失.如果删除失败,我需要以某种方式"回滚".我尝试过这样的事情.

        item.destroyRecord().then(function () {
            Notify.success("item removed");
        }).catch(function (response) {
            //NEED TO ROLLBACK HERE - ANY IDEAS?
            Notify.error('Failed to remove!');
        });
Run Code Online (Sandbox Code Playgroud)

Kin*_*n2k 7

首先,使用关系回滚在ember数据中并不完全有效,其次,较新版本的ember数据处理得更好(ember data 1.0 beta 7+).记录为此目的有一个回滚方法,它仍处于测试阶段,但它主要是你正在寻找的.

    item.destroyRecord().then(function () {
        Notify.success("item removed");
    }).catch(function (response) {
        item.rollback();
        Notify.error('Failed to remove!');
    });
Run Code Online (Sandbox Code Playgroud)

注意:在较新版本的Ember中,item.rollback()不再使用,而是使用item.rollbackAttributes()Marcelo的评论中提到的.

  • 如果服务器返回400,`item.rollback()`对我有效,但如果服务器返回422则失败.不知道为什么,但这可能对调试有帮助... (3认同)