我已经实现了一个有交易的路线。当用户通过单击“后退”按钮离开此路线时,我希望用户能够确认退出并丢失通过回滚事务所做的任何更改。
问题是,如果用户返回路由,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);
}
}),