2018-09-25 16:28:26 +00:00
|
|
|
import { isNone, isBlank } from '@ember/utils';
|
|
|
|
import { assign } from '@ember/polyfills';
|
|
|
|
import { decamelize } from '@ember/string';
|
2018-04-03 14:16:57 +00:00
|
|
|
import DS from 'ember-data';
|
|
|
|
|
|
|
|
export default DS.RESTSerializer.extend({
|
|
|
|
keyForAttribute: function(attr) {
|
|
|
|
return decamelize(attr);
|
|
|
|
},
|
|
|
|
|
|
|
|
pushPayload(store, payload) {
|
|
|
|
const transformedPayload = this.normalizeResponse(
|
|
|
|
store,
|
|
|
|
store.modelFor(payload.modelName),
|
|
|
|
payload,
|
|
|
|
payload.id,
|
|
|
|
'findRecord'
|
|
|
|
);
|
|
|
|
return store.push(transformedPayload);
|
|
|
|
},
|
|
|
|
|
|
|
|
normalizeItems(payload) {
|
2018-09-25 16:28:26 +00:00
|
|
|
assign(payload, payload.data);
|
2018-04-03 14:16:57 +00:00
|
|
|
delete payload.data;
|
|
|
|
return payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
|
|
|
|
const responseJSON = this.normalizeItems(payload);
|
|
|
|
const { modelName } = primaryModelClass;
|
|
|
|
let transformedPayload = { [modelName]: responseJSON };
|
|
|
|
let ret = this._super(store, primaryModelClass, transformedPayload, id, requestType);
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
|
|
|
|
serializeAttribute(snapshot, json, key, attributes) {
|
|
|
|
const val = snapshot.attr(key);
|
|
|
|
if (attributes.options.readOnly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
attributes.type === 'object' &&
|
|
|
|
val &&
|
|
|
|
Object.keys(val).length > 0 &&
|
2018-09-25 16:28:26 +00:00
|
|
|
isNone(snapshot.changedAttributes()[key])
|
2018-04-03 14:16:57 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-25 16:28:26 +00:00
|
|
|
if (isBlank(val) && isNone(snapshot.changedAttributes()[key])) {
|
2018-04-03 14:16:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._super(snapshot, json, key, attributes);
|
|
|
|
},
|
|
|
|
});
|