import Ember from 'ember';

const {
  get,
  Mixin
} = Ember;

export default Mixin.create({
  resetController() {
    let model = get(this, 'controller.model');

    if (!get(model, 'isDeleted')) {
      model.rollbackAttributes();
    }
  },
  actions: {
    willTransition(transition) {
      let model = get(this, 'controller.model');

      if (get(model, 'hasDirtyAttributes') && !this.willTransitionConfirm(transition)) {
        transition.abort();
      }
    }
  },
  willTransitionConfirm() {
    return true;
  }
});

import Ember from 'ember';

const {
  get,
  Mixin
} = Ember;

export default Mixin.create({
  primaryModel: 'model',
  resetController() {
    let primaryModel = get(this, 'primaryModel');
    let model = get(this, `controller.${primaryModel}`);

    if (!get(model, 'isDeleted')) {
      model.rollbackAttributes();
    }
  },
  actions: {
    willTransition(transition) {
      let primaryModel = get(this, 'primaryModel');
      let model = get(this, `controller.${primaryModel}`);

      if (get(model, 'hasDirtyAttributes') && !this.willTransitionConfirm(transition)) {
        transition.abort();
      }
    }
  },
  willTransitionConfirm() {
    return true;
  }
});

Add a primary model attribute
feat: Add a primary model attribute

`primaryModel` defaults to `'model'` but can be changed in cases where the `model` hook must return an array or hash of promises and the user wants to be explicit about the model in the template e.g. `user` vs `model`.