Ember Transactions:在事务之间移动对象

时间:2012-09-06 11:33:56

标签: transactions ember.js ember-data

我已经实现了一个有交易的Route。当用户通过单击“后退”按钮移出此路线时,我希望用户能够确认退出并丢失通过回滚事务所做的任何更改。

问题在于,如果用户返回路线,Ember Data会提出错误并说明:

Error: assertion failed: Models cannot belong to more than one transaction at a time.

即使我在旧事务上显式调用remove()并将add()添加到新事务中,也是如此(参见下面的newTransaction()函数):

settingsDetails: Ember.Route.extend({
    route: '/details',
    transaction: MyApp.store.transaction(),

    doBackButton: function() {
        var dirty = MyApp.router.get('settingsDetailsController.content.isDirty');
        var doTransition = true;
        if (dirty) {
            var confirmDialog = confirm('You have unsaved changes. Are you sure you want to continue ? \n\nAny chances made will be lost!');
            doTransition = confirmDialog;
        }

        if (doTransition) {
            this.doResetSettingsDetails();
            MyApp.router.transitionTo('settings.settingsOverview');
        }
    },

    newTransaction: function() {
        var oldTransaction = this.get('transaction');
        var newTransaction = MyApp.store.transaction();

        var record = MyApp.router.get('settingsDetailsController').get('content');
        if (record) {
            oldTransaction.remove(record);
            newTransaction.add(record);
        }
        this.set('transaction', newTransaction);
    },

    doUpdateSettingsDetails: function() {
        this.get('transaction').commit();
        this.newTransaction();
    },

    doResetSettingsDetails: function() {
        this.get('transaction').rollback();
        this.newTransaction();
    },

    connectOutlets: function(router) {
        router.get('applicationController').connectOutlet('settingsDetails');
        var record = MyApp.store.find(MyApp.PersonDetails, 1);
        this.get('transaction').add(record);
        router.get('settingsDetailsController').set('content', record);
    }
}),

1 个答案:

答案 0 :(得分:0)

自Ember Data的测试版以来,Ember Data不再拥有交易。因此,这个问题不再适用。