Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import JSONSerializer from '@ember-data/serializer/json';
import { underscore } from '@ember/string';
export default JSONSerializer.extend({
// Use camel-cased attribute names in the JS models, but underscore the
// attribute names for any server-side communication.
keyForAttribute(attr) {
return underscore(attr);
},
// For single records, look for the data under the customizable
// "singlePayloadKey" attribute name on the response.
normalizeSingleResponse(store, primaryModelClass, payload, id, requestType) {
let key = primaryModelClass.singlePayloadKey;
if(key) {
payload = payload[key];
}
return this._super(store, primaryModelClass, payload, id, requestType);
},
extractRelationship(relationshipHash) {
let normalizedRelationship = this._super(...arguments);
// Customize relationship meta
normalizedRelationship.meta = camelCaseKeys(normalizedRelationship.meta);
return normalizedRelationship;
}
});
@since 1.13.0 @class JSONAPISerializer @extends JSONSerializer / const JSONAPISerializer = JSONSerializer.extend({ /* @method _normalizeDocumentHelper @param {Object} documentHash @return {Object} @private */ _normalizeDocumentHelper(documentHash) { if (typeOf(documentHash.data) === 'object') { documentHash.data = this._normalizeResourceHelper(documentHash.data); } else if (Array.isArray(documentHash.data)) { let ret = new Array(documentHash.data.length);
for (let i = 0; i < documentHash.data.length; i++) {
let data = documentHash.data[i];
ret[i] = this._normalizeResourceHelper(data);
}
import JSONSerializer from '@ember-data/serializer/json';
import { EmbeddedRecordsMixin } from '@ember-data/serializer/rest';
export default JSONSerializer.extend(EmbeddedRecordsMixin, {});
export default RESTSerializer.extend({
keyForAttribute(attr, method) {
return underscore(attr).toUpperCase();
}
});
You can also implement keyForRelationship
, which takes the name
of the relationship as the first parameter, the kind of
relationship (hasMany
or belongsTo
) as the second parameter, and
the method (serialize
or deserialize
) as the third parameter.
@class RESTSerializer
@extends JSONSerializer
/
const RESTSerializer = JSONSerializer.extend({
/*
keyForPolymorphicType
can be used to define a custom key when
serializing and deserializing a polymorphic type. By default, the
returned key is ${key}Type
.
Example
```app/serializers/post.js
import RESTSerializer from '@ember-data/serializer/rest';
export default RESTSerializer.extend({
keyForPolymorphicType(key, relationship) {
var relationshipKey = this.keyForRelationship(key);
return 'type-' + relationshipKey;
}