Ghost/ghost/admin/app/components/modals/new-subscriber.js
Kevin Ansfield 43bf325800 use ember-ajax in place of ember-data's networking (#283)
closes #7014
- uses the `AjaxServiceSupport` mixin from `ember-ajax` to replace Ember Data's internal `ajax` method with our own ajax service
- normalizes all error handling to use `ember-ajax` style errors
- default to the `application/vnd.api+json` content-type so that we don't have a mix of urlencoded and plain JSON content
- fix `normalizeErrorResponse` in our `ajax` service so that it doesn't add an empty `errors` array to payloads
2016-09-26 11:59:04 -05:00

46 lines
1.6 KiB
JavaScript

import {A as emberA} from 'ember-array/utils';
import ModalComponent from 'ghost-admin/components/modals/base';
import {isInvalidError} from 'ember-ajax/errors';
export default ModalComponent.extend({
actions: {
updateEmail(newEmail) {
this.set('model.email', newEmail);
this.set('model.hasValidated', emberA());
this.get('model.errors').clear();
},
confirm() {
let confirmAction = this.get('confirm');
this.set('submitting', true);
confirmAction().then(() => {
this.send('closeModal');
}).catch((error) => {
// TODO: server-side validation errors should be serialized
// properly so that errors are added to the model's errors
// property
if (error && isInvalidError(error)) {
let [firstError] = error.errors;
let {message} = firstError;
if (message && message.match(/email/i)) {
this.get('model.errors').add('email', message);
this.get('model.hasValidated').pushObject('email');
return;
}
}
// this is a route action so it should bubble up to the global
// error handler
throw error;
}).finally(() => {
if (!this.get('isDestroying') && !this.get('isDestroyed')) {
this.set('submitting', false);
}
});
}
}
});