Serializer

A resource's service is a combination of an adapter, serializer and cache mixin. An initializer injects the service into the resource (model).

The initializer injects a serializer instance into the adapter instance for a resource.

You may customize a serializer to transform (serialize/deserialize) attributes for a resource. The serializer uses a naming convention to transform an attribute by type (e.g. date) or by attribute name.

Extending ApplicationSerializer

Customize a serializer prototype by passing an object when extending the application serializer.

import ApplicationSerializer from 'ember-jsonapi-resources/serializers/application';

export default ApplicationSerializer.extend({});

A serializer has methods to serialize, deserialize, serializeChanged, and serializeRelationships; and other related methods. The serializer also has methods to deserialize in resonse to a successful request from the application server.

Attribute Transforms

The conventions employed by a serializer first use a method based on an attribute name. Second, the serializer will use a method based on the type of the attribute. Define the methods for a serializer to use by extending the TransformMixin.

The application serializer includes a Date type transform. The serializer transforms a data attribute using the methods serializeDateAttribute and deserializeDateAttribute. The resource attribute can have any name, and is setup using the attr('date') helper.

A serializer may also use any transformations you define. The serializer uses a convention based on the attribute type to find the transform method, e.g. serializeUpdatedAtAttribute and deserializeUpdatedAtAttribute. In your resource use the custom type with the attr helper like so: attr('updated-at', false /*read only*/).

A method based on the name of the attribute takes precedence over a transform method based on the type of the attribute (e.g. date). In addition to the type transforms you use methods based on the name of an attribute, e.g. serializeStatusAttribute and deserializeStatusAttribute.

results matching ""

    No results matching ""