Files
threshold-ui/addons/api/addon/serializers/fragment-managed-group-attributes.js
T

27 lines
992 B
JavaScript
Raw Normal View History

2021-10-29 15:01:50 -07:00
import JSONSerializer from '@ember-data/serializer/json';
export default class FragmentManagedGroupAttributesSerializer extends JSONSerializer {
2021-10-29 15:01:50 -07:00
/**
* If an attribute is annotated as readOnly in the model, don't serialize it.
* 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, attribute) {
const { type, options } = attribute;
2021-10-29 15:01:50 -07:00
let value = super.serializeAttribute(...arguments);
2021-11-01 12:26:22 -07:00
// Convert empty string to null.
2021-10-29 15:01:50 -07:00
if (type === 'string' && json[key] === '') json[key] = null;
// Do not serialize read-only attributes.
if (options.readOnly) delete json[key];
// Version is sent only if it has a non-nullish value
if (key === 'version') {
if (json[key] === null || json[key] === undefined) delete json[key];
}
2021-10-29 15:01:50 -07:00
return value;
}
}