framework/ember/app/adapters/application.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-02-03 18:40:34 +10:30
import DS from 'ember-data';
import JsonApiAdapter from 'ember-json-api/json-api-adapter';
import config from 'flarum/config/environment';
import AlertMessage from 'flarum/components/ui/alert-message';
2014-12-20 16:56:46 +10:30
export default JsonApiAdapter.extend({
host: config.apiURL,
2015-02-03 18:40:34 +10:30
ajaxError: function(jqXHR) {
var errors = this._super(jqXHR);
// Reparse the errors in accordance with the JSON-API spec to fit with
// Ember Data style. Hopefully something like this will eventually be a
// part of the JsonApiAdapter.
2015-02-03 18:40:34 +10:30
if (errors instanceof DS.InvalidError) {
var newErrors = {};
for (var i in errors.errors) {
var error = errors.errors[i];
newErrors[error.path] = error.detail;
}
return new DS.InvalidError(newErrors);
2015-02-03 18:40:34 +10:30
}
// If it's a server error, show an alert message. The alerts controller
// has been injected into this adapter.
if (errors instanceof JsonApiAdapter.ServerError) {
var message;
if (errors.status === 401) {
message = 'You don\'t have permission to do this.';
} else {
message = errors.message;
}
2015-02-26 13:28:44 +10:30
var alert = AlertMessage.extend({
type: 'warning',
message: message
});
this.get('alerts').send('alert', alert);
}
2015-02-03 18:40:34 +10:30
return errors;
}
});