When data comes in and goes out, the JSON structure is serialized and deserialized. The adapter uses the serializer to get the data in and out of the store to build and resolve request/response data.
You can create a serializer
with ember g serializer [application or model name]
,
which will create a serializer file with boilerplate code, like this:
import DS from 'ember-data'; export default DS.JSONAPISerializer.extend({ });
The serializer is an object
assigned to the serializer
property in the
adapter. Without a specific serializer file in your
application, Ember will use a default adapter and serializer, the
JSONAPIAdapter
and JSONAPISerializer
.
When you include a new serializer for the application, that serializer will
be used as the defaultSerializer
for the corresponding app/adapters/application.js file. Using the model’s name as the command option in ember g serializer
allows you
customize serialization of data for a specific model.
As with the JSONAPIAdapter
, the configuration should only be
changed if your API does not conform to the JSONAPI
specification
or if you have strange edge cases. If, in your own project, you were to need changes to
request or response data, here are some methods to investigate in
the Ember Data documentation: keyForAttribute, keyForRelationship,
modelNameFromPayloadKey, and serialize.
keyForAttribute is a method to transform
attribute names from the model to a keyname sent in the request.
This method expects three arguments: key, typeClass, and method. For
the JSONAPISerializer
, this method returns the key
dasherized, meaning that any underscores or camelcasing in the keyname
will be converted to dashes. For example, if a model has a property
first_name
, the request object will have a key
of first-name
. If your API expects
first_name
, you will need to make a change
to your the keyForAttribute method to resolve
the naming issue.
keyForRelationship
follows the same process, only for relationship keys. If your
model names contain underscores you will need to edit
this method for JSONAPISerializer
when you have linked models
with belongsTo or
hasMany relationships. Some
APIs expect relationships to add a suffix of
_id
or _ids
for these
relationships. This is the method to make that change.
The bnr-tracker-api
you are using for Tracker uses the appropriate JSONAPI
key names with dashes, such as cryptid-type
.
You will not need to add a serializer in your app.
However, for illustration, here is an example of using the Ember utility method Ember.String.underscore
to change the attribute keys of the incoming and outgoing JSON data.
import Ember from 'ember'; import DS from 'ember-data'; var underscore = Ember.String.underscore; export default DS.JSONAPISerializer.extend({ keyForAttribute(attr) { return underscore(attr); }, keyForRelationship(rawKey) { return underscore(rawKey); } });
Ember provides a number of string manipulation methods on the
Ember.String
object.
Ember.String.underscore converts a string
with dashes or camelcase to use
an underscore to separate words.
The serializer methods are called during the lifecycle of a
request for data, such as this.store.findAll('witness')
.
One way to examine the callback flow of a data request would be to add a
debugger
statement in this code to see what
comes in and what goes out of the method.
Here is an example:
import Ember from 'ember'; import DS from 'ember-data'; var underscore = Ember.String.underscore; export default DS.JSONAPISerializer.extend({ keyForAttribute(attr) { let returnValue = underscore(rawKey); debugger; return returnValue;return underscore(rawKey);}, keyForRelationship(rawKey) { return underscore(rawKey); } });
When working with a new API, this style of debugging comes in handy.
You have access to the Ember.String
object for when
you need to manipulate the attr
argument to
serialize your key names. Thankfully, Ember and
the Ember community build adapters and
serializers for numerous API patterns.
18.220.88.62