我有一个使用 Band 实体的简单应用程序。我创建了用户可以更新现有记录的表单。它由这个控制器处理:
App.BandEditController = Ember.Controller.extend({
startEditing: function() {
var band = this.get('model');
var transaction = band.get('store').transaction();
transaction.add(band); <-- THIS IS THE PROBLEM (IN 2ND RUN)
this.transaction = transaction;
},
stopEditing: function() {
// rollback the local transaction if it hasn't already been cleared
var transaction = this.transaction;
if (transaction) {
transaction.rollback();
this.transaction = undefined;
}
this.startEditing();
},
save: function() {
this.transaction.commit();
this.transaction = undefined;
this.stopEditing();
},
cancel: function() {
this.stopEditing();
}
});
当我显示表单并执行一次编辑时,一切正常。但是,我想保持表单显示并允许用户再次编辑它。使用我的方法,在将更新的记录发送到服务器后,当该startEditing
方法再次尝试将波段添加到事务中时,我收到此错误:
Uncaught Error: assertion failed: Once a record has changed, you cannot move it into a different transaction
我知道我不能用同一个对象创建另一个事务。但为什么?我该如何解决这个问题?
(顺便说一句,有没有比那些事务、startEditing 和 stopEditing 方法更简单的方法来更新记录?这对我来说似乎有点矫枉过正。)