Issue with Ember Data and Creating a Record

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:

  1. rollback doesn’t exist anymore. Here I destroy the record with destroyRecord
  2. isDirty is also gone. I replaced it with isNew in this example
  3. resetController doesn’t seem to wait for the promise returned by destroyRecord, 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 :slight_smile: