7

我想使用 ember-data 创建一个对象,但我不想在调用 commit 之前保存它。我怎样才能实现这种行为?

4

3 回答 3

4

您可以在 transaction_test.js 中使用'transaction s, 定义的transaction.js和相应的测试。

在此处查看示例:

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​
于 2012-04-03T12:24:50.203 回答
1

改为调用 createModel !

例子:

// 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" });

我为您制作了这个http://jsfiddle.net/Qpkz5/269/

于 2012-04-03T12:29:15.753 回答
0

您可以使用_create: App.MyModel._create()- 它将模型与其自己的状态管理器相关联,因此App.store.commit()不会做任何事情。

然而,_create是“私人的”。我认为这个用例需要一个公共方法。

于 2012-05-20T04:50:49.853 回答