Toran Billups
Toran Billups

Reputation: 27399

How to prevent a model from being committed with ember-data?

I'm using the latest rev of ember-data and I have a typical situation where most of the models need to be managed by ember-data so when I do a commit on the store they sync up w/ the backend. In one particular case I have a model that is only used clientside (but I created this as a DS.Model because it has relationships with other ember-data models in the running app).

What can I mark on the model itself to make sure it never looks "dirty" or "new"

I tried doing something like this when the object is created but it's still being change tracked for some odd reason

App.Foo.createRecord({name: 'foo', loaded: true, new: false, dirty: false});

Upvotes: 2

Views: 708

Answers (1)

Andre Malan
Andre Malan

Reputation: 2043

You can add the model to it's own transaction.

transaction = this.get('store').transaction();
myObject = transaction.createRecord(App.Foo, {name: 'foo', loaded: true, new: false, dirty: false});
transaction.commit();
// or alternatively: myObject.transaction.commit()

A normal store.commit() will not affect objects in this transaction.

Ember actually creates a default transaction in the background, which is what gets committed when you call a naked this.get('store').commit();

You can also add existing records to a transaction by going:

foo = App.Foo.find(1);
transaction = this.get('store').transaction();
transaction.add(foo);
foo.set('name', 'bar');
transaction.commit();

If you don't want to commit the transaction and also don't want to keep the changes that you made in it lying around, you can just call:

transaction.rollback();

Upvotes: 2

Related Questions