Search code examples
ember.jsember-data

Ember Data delete fails, how to rollback


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

Solution

  • 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.