2

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});
Toran Billups
  • 27,111
  • 40
  • 155
  • 268

1 Answers1

2

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();
Andre Malan
  • 2,043
  • 12
  • 12
  • No worries, I'd just been working with them, so it was fresh in my mind. – Andre Malan Feb 07 '13 at 11:52
  • so it works great if I just don't do the last step you mention "transaction.commit" -if I do this it will try to do an http post for each item in that transaction (instead I just never want to commit these). Wrapping it inside my own transaction (instead of the default) does prevent a massive batch upload when I do a commit on the store (globally) so it seems to be what I was looking for (not sure of the perf implications (memory management) of holding all this in a transaction and never committing it)) – Toran Billups Feb 07 '13 at 12:11
  • Yeah, sorry that wasn't more clear, the commit is for if/when you want to commit things. Will update on how to clean things up using rollback as well. – Andre Malan Feb 07 '13 at 12:39