Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!');
        });
like image 951
Emad Avatar asked May 29 '14 16:05

Emad


1 Answers

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.

like image 93
Kingpin2k Avatar answered Oct 12 '22 23:10

Kingpin2k