open-vault/ui/app/serializers/pki-certificate.js
2018-04-03 09:16:57 -05:00

64 lines
1.7 KiB
JavaScript

import DS from 'ember-data';
import Ember from 'ember';
const { decamelize } = Ember.String;
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) {
if (payload.data && payload.data.keys && Array.isArray(payload.data.keys)) {
let ret = payload.data.keys.map(key => {
let model = {
id_for_nav: `cert/${key}`,
id: key,
};
if (payload.backend) {
model.backend = payload.backend;
}
return model;
});
return ret;
}
Ember.assign(payload, payload.data);
delete payload.data;
return payload;
},
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
const responseJSON = this.normalizeItems(payload);
const { modelName } = primaryModelClass;
let transformedPayload = { [modelName]: responseJSON };
return this._super(store, primaryModelClass, transformedPayload, id, requestType);
},
serializeAttribute(snapshot, json, key, attributes) {
const val = snapshot.attr(key);
const valHasNotChanged = Ember.isNone(snapshot.changedAttributes()[key]);
const valIsBlank = Ember.isBlank(val);
if (attributes.options.readOnly) {
return;
}
if (attributes.type === 'object' && val && Object.keys(val).length > 0 && valHasNotChanged) {
return;
}
if (valIsBlank && valHasNotChanged) {
return;
}
this._super(snapshot, json, key, attributes);
},
});