open-vault/ui/app/serializers/node.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

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(DS.EmbeddedRecordsMixin, {
keyForAttribute: function(attr) {
return decamelize(attr);
},
pushPayload(store, payload) {
const transformedPayload = this.normalizeResponse(
store,
store.modelFor('node'),
payload,
null,
'findAll'
);
return store.push(transformedPayload);
},
nodeFromObject(name, payload) {
const nodeObj = payload.nodes[name];
return assign(nodeObj, {
2018-04-03 14:16:57 +00:00
name,
id: name,
});
},
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
const nodes = payload.nodes
? Object.keys(payload.nodes).map(name => this.nodeFromObject(name, payload))
: [assign(payload, { id: '1' })];
2018-04-03 14:16:57 +00:00
const transformedPayload = { nodes: nodes };
return this._super(store, primaryModelClass, transformedPayload, id, requestType);
},
normalize(model, hash, prop) {
hash.id = '1';
return this._super(model, hash, prop);
},
});