2017-12-15 21:39:18 +00:00
|
|
|
import { makeArray } from '@ember/array';
|
2017-09-19 14:47:10 +00:00
|
|
|
import JSONSerializer from 'ember-data/serializers/json';
|
|
|
|
|
|
|
|
export default JSONSerializer.extend({
|
|
|
|
primaryKey: 'ID',
|
|
|
|
|
|
|
|
keyForAttribute(attr) {
|
|
|
|
return attr.camelize().capitalize();
|
|
|
|
},
|
|
|
|
|
|
|
|
keyForRelationship(attr, relationshipType) {
|
2017-10-13 22:17:51 +00:00
|
|
|
const key = `${attr
|
|
|
|
.singularize()
|
|
|
|
.camelize()
|
|
|
|
.capitalize()}ID`;
|
2017-09-19 14:47:10 +00:00
|
|
|
return relationshipType === 'hasMany' ? key.pluralize() : key;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Modeled after the pushPayload for ember-data/serializers/rest
|
|
|
|
pushPayload(store, payload) {
|
|
|
|
const documentHash = {
|
|
|
|
data: [],
|
|
|
|
included: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(payload).forEach(key => {
|
|
|
|
const modelName = this.modelNameFromPayloadKey(key);
|
|
|
|
const serializer = store.serializerFor(modelName);
|
|
|
|
const type = store.modelFor(modelName);
|
|
|
|
|
|
|
|
makeArray(payload[key]).forEach(hash => {
|
|
|
|
const { data, included } = serializer.normalize(type, hash, key);
|
|
|
|
documentHash.data.push(data);
|
|
|
|
if (included) {
|
|
|
|
documentHash.included.push(...included);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
store.push(documentHash);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
modelNameFromPayloadKey(key) {
|
|
|
|
return key.dasherize().singularize();
|
|
|
|
},
|
|
|
|
});
|