Transactions 余烬数据:如何在控制器操作之间共享和更新事务中的对象?

Transactions 余烬数据:如何在控制器操作之间共享和更新事务中的对象?,transactions,ember.js,nested-attributes,ember-data,Transactions,Ember.js,Nested Attributes,Ember Data,我在GitHub上找到了一个很好的ember数据工作示例,并尝试通过嵌套资源('has_many:comments')对其进行扩展。在原始示例中,每次打开“编辑”视图时都会创建一个新事务,如果离开编辑模式,则会提交/回滚该事务 我想在content.comments中添加一条新的注释。注释我不能这样做,并且出现错误,因为“内容”已经在事务中(错误:断言失败:一旦记录发生更改,您就不能将其移动到其他事务中) 我试图意识到的想法是错误的,我必须采取另一种方式吗 App.EditContactCont

我在GitHub上找到了一个很好的ember数据工作示例,并尝试通过嵌套资源('has_many:comments')对其进行扩展。在原始示例中,每次打开“编辑”视图时都会创建一个新事务,如果离开编辑模式,则会提交/回滚该事务

我想在content.comments中添加一条新的注释。注释我不能这样做,并且出现错误,因为“内容”已经在事务中(错误:断言失败:一旦记录发生更改,您就不能将其移动到其他事务中)

我试图意识到的想法是错误的,我必须采取另一种方式吗

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

我想你可以从我所做的事情中获得灵感:

也许只需执行
this.get('content.comments').createRecord({body:'})
即可。 此调用引用ManyArray.createRecord(),并使用关系所有者的事务创建新记录。看