1

I have simple app working with Band entities. I have created the form where the user can update existing record. It's handled by this controller:

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

It's hugely inspired by this: https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/controllers/contact_edit_controller.js

When I display the form and perform the editing once, everything's OK. However, I'd like to keep the form displayed and allow the user to edit it again. With my approach, immediately after sending the updated record to the server, when the startEditing method tries to add the band to the transaction again, I get this error:

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

I understand I cannot create another transaction with the same object. But why? And how can I fix this?

(Btw, is there any simplier approach for updating the records than those transactions, startEditing and stopEditing methods? It seems a bit overkill to me.)

4

1 に答える 1

0

あなたはthis.startEditing();から電話をかけていますstopEditing-これは奇妙に思えます。元のコードにはそのような行はありません。他の場所から呼び出すとstartEditing、2回呼び出されます。

また、AFAIKでは、モデルを再度変更する前に、サーバーの応答を待つ必要があります。

于 2013-02-11T12:58:07.143 に答える