refactor: send scope_id query param on scope create, rather than serializing into the payload

This commit is contained in:
Randall Morey committed 2020-07-23 19:01:31 -04:00
1 parent 1b7a8902df
commit d08d52e090
7 files changed
+52 -71

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
import ApplicationAdapter from './application';
import { serializeIntoHash } from '@ember-data/adapter/-private';
export default class ScopeAdapter extends ApplicationAdapter {
/**
* Identical to default method except that we pass a query parameter
* indicating the parent scope.
* @override
* @param {Store} store
* @param {Model} type
* @param {Snapshot} snapshot
* @return {Promise} promise
*/
createRecord(store, type, snapshot) {
const parentScopeID = snapshot.attr('scope').attr('scope_id');
let url =
this.buildURL(type.modelName, null, snapshot, 'createRecord');
// TODO this should be generalized into `buildURL`, but currently Ember Data
// doesn't provide a way to manage query parameters. They are passed on
// to the underlying ajax/fetch service.
url = `${url}?scope_id=${parentScopeID}`;
const data = serializeIntoHash(store, type, snapshot);
return this.ajax(url, 'POST', { data });
}
}
-36
View File
@@ -1,36 +0,0 @@
import ApplicationSerializer from './application';
export default class ScopeSerializer extends ApplicationSerializer {
// =attributes
attrs = {
scope: { serialize: true }
};
// =methods
/**
* While most resources do not serialize scope into a request body (scope
* is expressed in the URL), scopes themselves _do serialize scope_ as a
* string field `scope_id`.
* Otherwise delegate to default attribute serializer.
*
* @override
* @method serializeAttribute
* @param {Snapshot} snapshot
* @param {Object} json
* @param {String} key
* @param {Object} attribute
*/
serializeAttribute(snapshot, json, key) {
const value = super.serializeAttribute(...arguments);
const parentScope = snapshot.attr('scope');
if (key === 'scope' && this._canSerialize(key) && parentScope) {
json.scope_id = parentScope.attr('scope_id');
delete json[key];
}
return value;
}
}