parent
4dbfb73b3c
commit
58cbf4027d
3 changed files with 47 additions and 27 deletions
31
app/mixins/save-model.js
Normal file
31
app/mixins/save-model.js
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
import Ember from 'ember';
|
||||||
|
import ajaxError from '../utils/ajax-error';
|
||||||
|
|
||||||
|
export default Ember.Mixin.create({
|
||||||
|
actions: {
|
||||||
|
save: function() {
|
||||||
|
const model = this.get('model');
|
||||||
|
const fallbackRoute = this.get('fallbackRoute');
|
||||||
|
|
||||||
|
if (model.get('hasDirtyAttributes')) {
|
||||||
|
model.save().then((model) => {
|
||||||
|
this.transitionToRoute(fallbackRoute, model);
|
||||||
|
}, () => {
|
||||||
|
ajaxError(model.get('errors'), this.get('flashMessages'));
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.transitionToRoute(fallbackRoute, model);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
cancel: function() {
|
||||||
|
const model = this.get('model');
|
||||||
|
const fallbackRoute = this.get('fallbackRoute');
|
||||||
|
|
||||||
|
model.get('errors').clear();
|
||||||
|
model.rollbackAttributes();
|
||||||
|
|
||||||
|
this.transitionToRoute(fallbackRoute, model);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
|
@ -1,32 +1,9 @@
|
||||||
import Ember from 'ember';
|
import Ember from 'ember';
|
||||||
import ajaxError from '../../../../utils/ajax-error';
|
import SaveModel from '../../../../mixins/save-model';
|
||||||
|
|
||||||
const { Controller } = Ember;
|
const { Controller } = Ember;
|
||||||
|
|
||||||
export default Controller.extend({
|
export default Controller.extend(SaveModel, {
|
||||||
actions: {
|
// Required for SaveModel mixin
|
||||||
save: function() {
|
fallbackRoute: 'protected.species.show',
|
||||||
const model = this.get('model');
|
|
||||||
|
|
||||||
if (model.get('hasDirtyAttributes')) {
|
|
||||||
model.save().then((model) => {
|
|
||||||
this.transitionToRoute('protected.species.show', model);
|
|
||||||
}, () => {
|
|
||||||
ajaxError(model.get('errors'), this.get('flashMessages'));
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.transitionToRoute('protected.species.show', model);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
cancel: function() {
|
|
||||||
const model = this.get('model');
|
|
||||||
|
|
||||||
model.get('errors').clear();
|
|
||||||
model.rollbackAttributes();
|
|
||||||
|
|
||||||
this.transitionToRoute('protected.species.show', model);
|
|
||||||
},
|
|
||||||
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
12
tests/unit/mixins/save-model-test.js
Normal file
12
tests/unit/mixins/save-model-test.js
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import Ember from 'ember';
|
||||||
|
import SaveModelMixin from '../../../mixins/save-model';
|
||||||
|
import { module, test } from 'qunit';
|
||||||
|
|
||||||
|
module('Unit | Mixin | save model');
|
||||||
|
|
||||||
|
// Replace this with your real tests.
|
||||||
|
test('it works', function(assert) {
|
||||||
|
var SaveModelObject = Ember.Object.extend(SaveModelMixin);
|
||||||
|
var subject = SaveModelObject.create();
|
||||||
|
assert.ok(subject);
|
||||||
|
});
|
Reference in a new issue