I have an ember app with a data model and the usual CRUD views/routes/controllers for listing and adding new items.
When I click 'add new' from the index page, I get the expected form, with 'Save' and 'Cancel' buttons. If I press cancel, the view goes back to the index view and the model is not saved, as expected.
This is the code of the new items controller:
App.ItemsNewController = Ember.ObjectController.extend({
actions: {
saveChanges: function() {
this.model.save();
this.transitionTo('items');
},
cancel: function() {
this.model.rollback(); // how to run this when the back button is pressed?
this.transitionTo('items');
},
},
});
But if I press the back button, I also end up in the index page, but an extra empty row appears in the index listing, corresponding to the new record that was not saved in the 'new' view. This record was not persisted to the data layer, but it somehow still exists in memory, and this.store.find('items')
returns a collection that includes it. If I refresh the page, the blank row disappears. Also, as stated above, if I exit the form using the cancel button, the record is discarded, because in the 'cancel' action method I invoke this.model.rollback()
.
So bottomline, I guess I would have to rollback the model when the back button is pressed, but there's no route callback that I know of to hook some code to this event.
In case it's important, I'm using Ember 1.9.1, Ember Data 1.0.0-beta.15-canary, Handlebars 2.0.0, jQuery 1.10.2