create temporarty non persistent object in Ember-Data

流过昼夜 提交于 2020-01-10 02:57:32

问题


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?


回答1:


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​



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/9992652/create-temporarty-non-persistent-object-in-ember-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!