I implemented a route that has a transaction. When the user navigated from this route by pressing the back button, I want the user to be able to confirm the exit and lose any changes made by rolling back the transaction.
The problem is that if the user returns to the route, Ember Data picks up and erroneously reports that:
Error: assertion failed: Models cannot belong to more than one transaction at a time.
This is even though I explicitly call remove () in the old transaction and add () to the new transaction (see the newTransaction () function below):
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);
}
}),
source
share