Improve client XHR error handling

The default XHR error handler produce an alert which is appropriate to the response status code. It can be overridden per-request (by specifying the `errorHandler` option) so that the alert can be suppressed or displayed in a different position (e.g. inside a modal).

ref #118
This commit is contained in:
Toby Zerner
2015-10-20 12:48:26 +10:30
parent 7490709af8
commit 26a821e3e2
26 changed files with 192 additions and 175 deletions

View File

@ -117,10 +117,11 @@ export default class Model {
*
* @param {Object} attributes The attributes to save. If a 'relationships' key
* exists, it will be extracted and relationships will also be saved.
* @param {Object} [options]
* @return {Promise}
* @public
*/
save(attributes) {
save(attributes, options = {}) {
const data = {
type: this.data.type,
id: this.data.id,
@ -153,11 +154,11 @@ export default class Model {
this.pushData(data);
return app.request({
return app.request(Object.assign({
method: this.exists ? 'PATCH' : 'POST',
url: app.forum.attribute('apiUrl') + this.apiEndpoint(),
data: {data}
}).then(
}, options)).then(
// If everything went well, we'll make sure the store knows that this
// model exists now (if it didn't already), and we'll push the data that
// the API returned into the store.
@ -181,17 +182,18 @@ export default class Model {
* Send a request to delete the resource.
*
* @param {Object} data Data to send along with the DELETE request.
* @param {Object} [options]
* @return {Promise}
* @public
*/
delete(data) {
delete(data, options = {}) {
if (!this.exists) return m.deferred.resolve().promise;
return app.request({
return app.request(Object.assign({
method: 'DELETE',
url: app.forum.attribute('apiUrl') + this.apiEndpoint(),
data
}).then(() => {
}, options)).then(() => {
this.exists = false;
this.store.remove(this);
});