Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create temporarty non persistent object in Ember-Data

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?

like image 962
Krzysztof Kaczmarek Avatar asked Apr 03 '12 11:04

Krzysztof Kaczmarek


2 Answers

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​
like image 195
pangratz Avatar answered Sep 21 '22 03:09

pangratz


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.

like image 21
Luan Avatar answered Sep 20 '22 03:09

Luan