【发布时间】:2012-12-19 13:01:15
【问题描述】:
我在 GitHub 上找到了 https://github.com/dgeb/ember_data_example 下 ember-data 的一个很好的工作示例,并尝试通过嵌套资源('has_many: cmets')对其进行扩展。在原始示例中,每次打开编辑视图时都会创建一个新事务,如果离开编辑模式,则会提交/回滚。
我想在 content.cmets 中添加一条新注释我不能这样做并且出现错误,因为“内容”已经在事务中(错误:断言失败:一旦记录发生更改,您就无法将其移入不同的交易)。
我试图实现的想法是错误的,我必须采取其他方式吗?
App.EditContactController = Em.Controller.extend({
content: null,
addComment: function () {
// ERROR here:
this.get('content.comments').addObject(App.Comment.createRecord({body: ''}));
},
enterEditing: function() {
this.transaction = this.get('store').transaction();
if (this.get('content.id')) {
this.transaction.add(this.get('content'));
} else {
this.set('content', this.transaction.createRecord(App.Contact, {}));
}
},
exitEditing: function() {
if (this.transaction) {
this.transaction.rollback();
this.transaction = null;
}
},
updateRecord: function() {
// commit and then clear the transaction (so exitEditing doesn't attempt a rollback)
this.transaction.commit();
this.transaction = null;
}
});
【问题讨论】:
标签: transactions ember.js nested-attributes ember-data