4
votes

What is the recommended way of discarding changes made to records?

I have the following logic to revert dirty records to their original state.

if controller.get('isDirty')
  controller.get('content').rollback()

This works unless an unsuccessful attempt has been made to commit the record.

If I try to commit the changes and the server responds with errors, it's no longer possible to rollback the record this way. In this case, does Ember Data or the RESTAdapter provide a built-in method of reverting the record to its original state?


I'm using the packaged DS.RESTAdapter with Ember Data revision 11

2
I have the same problem is. Does this happen because of a problem in Ember-data?joscas

2 Answers

2
votes

I've found something that apparently works although I don't know why. Here is what I do in my model:

App.User = DS.Model.extend({

  becameInvalid: function(errors) {
    this.get('transaction').rollback();
    //this.rollback(); <- This doesn't work. You get becameClean error.
  }
});

The comment from sly7-7 for that issue gave me the idea.

1
votes

create/update your record through a transaction in a router. See more details here on how to do that.

var transaction = App.store.transaction()
transaction.createRecord(App.Foo);
transaction.commit()
transaction.rollback()