Skip to content

Instantly share code, notes, and snippets.

@visualjeff
Last active July 30, 2016 22:16
Show Gist options
  • Save visualjeff/f6ebbe05d24939a71caddbbaab218ff2 to your computer and use it in GitHub Desktop.
Save visualjeff/f6ebbe05d24939a71caddbbaab218ff2 to your computer and use it in GitHub Desktop.
Ember-Data Serializer Notes
Ember Data Notes:
Serializers
Either you are:
1. deserialization - data from the server, extract then normalize
2. serialization - Sending data to the server
You can define an alternate primary key in the serializer:
primaryKey: 'id',
NOTE: extractMeta doesn't seem to get called for JSONAPI Serializer. RESTFul Serializer yes.
Deserialization:
================
keyFor methods - For fixing things in your api and doing it all in one place.
attrs hash - An easy way to deal with differing client / server keys.
attrs: {
'clientKey': 'serverKey' //But this will grow as the number if there are several keys with issues.
}
//How about a single method to address differing client /server keys?
keyForAttribute(key, method) { //method by default is deserialization. Could change to serialization
return `${this._super(...arguments)}_attribute`
}
key parameter is on the ember side or client side
keyForRelationship(key, relationship, method) { //method by default is deserialization. Could change to serialization
return this._super(...arguments).toUpperCase();
}
normalizeResponse (overkill. Covers response as a whole).
Example usage is change a type of return object.
Default behavior is a big switch statement.
Called when you receive the entire response.
Better off using the other normize methods (such as):
normalizeArrayResponse
normalizeSingleResponse
normalize${specific}Response (Covers response as a whole)
normalizeSingleResponse
normalizeBelongsToResponse
...
normalizeArrayResponse
normalizeSaveResponse -> normalizeSingleResponse
normalizeFindAllResponse - to peal meta data out of a response.
---
Type is the one place where ember-data doesn't have your back
You need to address this in every single normalizeResponse
---
Normalize (covers individual JSON object)
Useful for changing things like the id
But normalize contains a lot of submethods
extractid (for correcting id's. Must return id)
extractAttributes (for correcting attributes. Must return attributes)
---
Be sure to call super before applying any customization: this._super(...arguments);
---
Serialization:
==============
1. serializeIntoHash - wraps all of the serialize calls. Empty object hash is returned with data.
2. serialize - wraps the three method below
3. serializeAttribute - adds in the attributes hash
4. serializeBelongsTo
5. serializeHasMany
How I've augmented serialization in the past:
serialize - Removed _rev from a payload when its null. Used this in BACT.
NOTE: Changing the name of attributes ==> Easiest way is to use the attrs hash.
Only add in the attribute you need to change. Easier than extractAttributes.
Try this test:
==============
Put a normalizeResponse and normalize in your serializer to see how often they are called.
`JSONSerializer` will normalize the JSON payload to the JSON API format that the
Ember Data store expects.
You can customize how JSONSerializer processes its payload by passing options in
the `attrs` hash or by subclassing the `JSONSerializer` and overriding hooks:
- To customize how a single record is normalized, use the `normalize` hook.
- To customize how `JSONSerializer` normalizes the whole server response, use the
`normalizeResponse` hook.
- To customize how `JSONSerializer` normalizes a specific response from the server,
use one of the many specific `normalizeResponse` hooks.
- To customize how `JSONSerializer` normalizes your id, attributes or relationships,
use the `extractId`, `extractAttributes` and `extractRelationships` hooks.
The `JSONSerializer` normalization process follows these steps:
- `normalizeResponse` - entry method to the serializer.
- `normalizeCreateRecordResponse` - a `normalizeResponse` for a specific operation is called.
- `normalizeSingleResponse`|`normalizeArrayResponse` - for methods like `createRecord` we expect
a single record back, while for methods like `findAll` we expect multiple methods back.
- `normalize` - `normalizeArray` iterates and calls `normalize` for each of its records while `normalizeSingle`
calls it once. This is the method you most likely want to subclass.
- `extractId` | `extractAttributes` | `extractRelationships` - `normalize` delegates to these methods to
turn the record payload into the JSON API format.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment