As of Ember-Data 2.1, the above solution is not accurate. Here’s an update, for an route called players/new
, which is used to create new instances of the model player
:
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.createRecord('player');
},
actions: {
save() {
const record = this.modelFor('players.new');
record.save()
.then(() => {
this.transitionTo('root.show');
})
.catch(error => {
console.error("Error saving player", error);
});
},
willTransition() {
const record = this.modelFor('players.new');
if (record.get('isNew')) {
return record.destroyRecord();
}
},
},
});
Points of note:
-
rollback
doesn’t exist anymore. Here I destroy the record withdestroyRecord
-
isDirty
is also gone. I replaced it withisNew
in this example -
resetController
doesn’t seem to wait for the promise returned bydestroyRecord
, meaning the record will still exist when the new route is loaded. Fortunately,willTransition
does wait for you
Unfortunately, 1 and 2 only make sense when creating records, not updating them. At the moment my app only creates, so I haven’t had to dig into that problem just yet.
I’d love to hear suggestions on how this can be improved