[英]Ember-data: how to share and update a object in transaction between controller actions?
I've found on GitHub a good working example for ember-data under https://github.com/dgeb/ember_data_example and try to extend it by nested resource ('has_many: comments'). 我在GitHub上找到了https://github.com/dgeb/ember_data_example下的ember-data的一个很好的工作示例,并尝试通过嵌套资源('has_many:comments')扩展它。 In the original example a new transaction is created every time the edit view is on and it is submitted/rolled back if the edit mode is leaved.
在原始示例中,每次打开编辑视图时都会创建一个新事务,如果编辑模式处于离开状态,则会提交/回滚该事务。
I wand to add a new comment into content.comments I can't do it and have the error because the 'content' is already in transaction (Error: assertion failed: Once a record has changed, you cannot move it into a different transaction). 我想在content.comments中添加一条新评论我无法做到并且因为“内容”已经在事务中而出现错误(错误:断言失败:一旦记录发生变化,您就无法将其移动到另一个事务中)。
Is the idea I try to realize wrong and I must take another way? 这个想法我试图意识到错了,我必须采取另一种方式吗?
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;
}
});
I think you could take inspiration from what I did: https://github.com/sly7-7/ember_data_example/commit/57ee7ea6ca44e3a2fbba96fff4ad088a8d786a3c 我想你可以从我做的事情中获取灵感: https : //github.com/sly7-7/ember_data_example/commit/57ee7ea6ca44e3a2fbba96fff4ad088a8d786a3c
Perhaps simply doing this.get('content.comments').createRecord({body: ''})
will work. 也许只是这样做
this.get('content.comments').createRecord({body: ''})
将起作用。 This call refers to the ManyArray.createRecord(), and use the transaction of the owner of the relationship to create the new record. 此调用引用ManyArray.createRecord(),并使用关系所有者的事务来创建新记录。 see https://github.com/sly7-7/data/blob/master/packages/ember-data/lib/system/record_arrays/many_array.js#L163
请参阅https://github.com/sly7-7/data/blob/master/packages/ember-data/lib/system/record_arrays/many_array.js#L163
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.