7

I want create an object using ember-data, but I don't want to save it until I call commit. How can I achieve this behavior?

Ankur Chauhan
  • 1,393
  • 2
  • 17
  • 37

3 Answers3

4

You can use transaction's, defined transaction.js with corresponding tests in transaction_test.js.

See an example here:

App.store = DS.Store.create(...);

App.User = DS.Model.extend({
    name: DS.attr('string')
});

var transaction = App.store.transaction();
transaction.createRecord(App.User, {
    name: 'tobias'
});

App.store.commit(); // does not invoke commit
transaction.commit(); // commit on store is invoked​
pangratz
  • 15,875
  • 7
  • 50
  • 75
1

Call createModel instead!

Example:

// This is a persisted object (will be saved upon commit)
var persisted = App.store.createRecord(App.Person,  { name: "Brohuda" });

// This one is not associated to a store so it will not
var notPersisted = App.store.createModel(App.Person,  { name: "Yehuda" });

I've made this http://jsfiddle.net/Qpkz5/269/ for you.

Luan
  • 402
  • 3
  • 6
  • You are using `ember-latest.js` from the downloads section of the data repository. This file has been uploaded 2 months ago (2011-01-30) and is outdated. I haven't found this method in the code from master. – pangratz Apr 03 '12 at 12:34
  • @pangratz is right, I hadn't notice that this was changed. Kudos him – Luan Apr 03 '12 at 12:46
0

You can use _create: App.MyModel._create() - it will associate the model with its own state manager, so App.store.commit() won't do anything.

However, _create is "private". I think there needs to be a public method for this use case.

Justin Ko
  • 271
  • 2
  • 4