简体   繁体   English

在Ember-Data中创建临时非持久对象

[英]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. 我想用ember-data创建一个对象,但我不想保存它直到我调用commit。 How can I achieve this behavior? 我怎样才能实现这种行为?

You can use transaction 's, defined transaction.js with corresponding tests in transaction_test.js . 您可以使用transaction的定义transaction.js与相应的测试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​

Call createModel instead! 改为调用createModel!

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. 我为你做了这个http://jsfiddle.net/Qpkz5/269/

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. 您可以使用_createApp.MyModel._create() - 它将模型与其自己的状态管理器相关联,因此App.store.commit()将不会执行任何操作。

However, _create is "private". 但是, _create是“私有的”。 I think there needs to be a public method for this use case. 我认为这个用例需要有一个公共方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM