我正在尝试在具有两个 belongsTo 关系的模型上管理 ember-data 事务(创建和提交记录)。最初,我只是使用(在 Coffeescript 中)将新记录添加到交易中:
@transaction.createRecord(App.Book, {author: App.router.authorController.get('content')})
这将作者属性(这是一个 DS.belongsTo 关系)设置为当前作者,然后用户在一个表单中设置书籍的其他属性,包括来自选择元素(假设是出版商)的另一个 belongsTo 关系。这工作正常,但如果用户去创建第二条新记录,我会得到以下断言:
Ember.assert("All records in a changed relationship must be in the same transaction. You tried to change the relationship between records when one is in " + t + " and the other is in " + prev, t === prev);
这是来自Ember Data 中 one_to_many_change.js 文件的第 203 行
我认为这意味着我必须手动将所有相关模型对象添加到事务中。因此,我可以使用以下方式将作者模型添加到事务中:
author = App.router.authorController.get('content')
@transaction.add(author)
然后对事务中可能受影响的任何发布者模型执行相同的操作(我在控制台中执行这些操作)。但是,当我这样做时,我会收到以下错误:
Error: assertion failed: Once a record has changed, you cannot move it into a different transaction
即使先前的(原始)事务已提交,也会发生这种情况。只是想知道其他人是如何处理这个问题的。我想在交易中分配这种关系必须是 Ember Data 应用程序中的常见过程。