Marco
Marco

Reputation: 4395

Ember Data and dirty records

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

Upvotes: 4

Views: 2711

Answers (2)

joscas
joscas

Reputation: 7674

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.

Upvotes: 2

ken
ken

Reputation: 3745

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()

Upvotes: 1

Related Questions