1

我有一个使用 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();
    }
});

它受到了极大的启发:https ://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/controllers/contact_edit_controller.js

当我显示表单并执行一次编辑时,一切正常。但是,我想保持表单显示并允许用户再次编辑它。使用我的方法,在将更新的记录发送到服务器后,当该startEditing方法再次尝试将波段添加到事务中时,我收到此错误:

Uncaught Error: assertion failed: Once a record has changed, you cannot move it into a different transaction 

我知道我不能用同一个对象创建另一个事务。但为什么?我该如何解决这个问题?

(顺便说一句,有没有比那些事务、startEditing 和 stopEditing 方法更简单的方法来更新记录?这对我来说似乎有点矫枉过正。)

4

1 回答 1

0

你是从哪里来this.startEditing();stopEditing——这看起来很奇怪。原始代码中没有这样的行。如果您startEditing从其他地方调用,这将导致它被调用两次..

另外,AFAIK,您需要等待服务器响应才能再次更改模型。

于 2013-02-11T12:58:07.143 回答