Matthew Gillingham
Matthew Gillingham

Reputation: 3429

Rolling back a deletion to handle a server error in Ember.js

We have an Ember.js application which uses Ember Data. We are trying to do the following:

  1. Delete a record.
  2. If there is a server error (due to the fact that the application can have a "locked" state where records can not be deleted), roll the record back to its previous state, prompt the user to unlock app, and continue.
  3. If there is no server error, continue as normal.

We have found that this does not work

object.destroyRecord().then ->
  # handle success
, (reason)->
  object.rollback()
  # prompt for the unlock

In both cases, we see an error that looks like:

Error: Assertion Failed: calling set on destroyed object

But it isn't clear how to remove the isDestroyed state once it has been set.

In general, it seems that, in either case, once we call destroyRecord, there is no way to rollback the changes to a pre-deleted state once, even if there is a server error.

Upvotes: 4

Views: 706

Answers (2)

James Jackson
James Jackson

Reputation: 788

I've found that you need to put the rollback call in becameError() function.

// Overwrite default destroyRecord
destroyRecord: function () {
    this.deleteRecord();
    this.save().then(
        function (){
            //Success
        },
        function () {
            //Failure
        }
    );
},

becameError: function (item) {
    this.rollback();
}

The item will disappear from views until the server returns the error and then magically reappear.

Upvotes: 0

Martin Stannard
Martin Stannard

Reputation: 811

Try deleteRecord, followed by save. The docs explicitly state that this allows you to rollback on error.

  object.deleteRecord()
  object.save().then( ->
     # handle success
  , (reason) ->
     object.rollback()
  )

Upvotes: 1

Related Questions