Merge branch 'develop' into ICU-350-users-crud
This commit is contained in:
11 files changed
+163
-3
No files matched your search
@@ -6,6 +6,36 @@ import { dasherize } from '@ember/string';
|
||||
import { pluralize } from 'ember-inflector';
|
||||
import { isArray } from '@ember/array';
|
||||
|
||||
/**
|
||||
* Returns true if the payload is an empty object, false otherwise.
|
||||
* Taken from Ember Data fetch-manager and "positivized" the name.
|
||||
* @param {object} adapterPayload
|
||||
* @return {boolean}
|
||||
*/
|
||||
function payloadIsBlank(adapterPayload) {
|
||||
if (Array.isArray(adapterPayload)) {
|
||||
return false;
|
||||
} else {
|
||||
return Object.keys(adapterPayload || {}).length === 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the resonse is empty, returns a new payload containing an empty `items`
|
||||
* array, as expected by the serializer. Otherwise, returns the response.
|
||||
*
|
||||
* Normally this would be performed 100% inside the serializer.
|
||||
* However, the fetch manager, which we cannot easily override, glues the
|
||||
* adapter and serializers together. Before it forwards a response from the
|
||||
* adapter on to the serializer, it checks for an "empty" response. If the
|
||||
* response is empty, fetch manager throws and error and never forwards it on.
|
||||
* @param {object} response
|
||||
* @return {object}
|
||||
*/
|
||||
function prenormalizeArrayResponse(response) {
|
||||
return payloadIsBlank(response) ? {items: []} : response;
|
||||
}
|
||||
|
||||
export default class ApplicationAdapter extends RESTAdapter {
|
||||
// =attributes
|
||||
|
||||
@@ -36,6 +66,26 @@ export default class ApplicationAdapter extends RESTAdapter {
|
||||
return dasherize(pluralize(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercepts "empty" responses and adds an empty `items` array.
|
||||
* @override
|
||||
* @method findAll
|
||||
* @return {Promise} promise
|
||||
*/
|
||||
findAll() {
|
||||
return super.findAll(...arguments).then(prenormalizeArrayResponse);
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercepts "empty" responses and adds an empty `items` array.
|
||||
* @override
|
||||
* @method query
|
||||
* @return {Promise} promise
|
||||
*/
|
||||
query() {
|
||||
return super.query(...arguments).then(prenormalizeArrayResponse);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides default ajax method by rewritting PUT requests as PATCH.
|
||||
* PATCH is the request method used by our API for updates.
|
||||
|
||||
Reference in new issue
Block a user