Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solves #4186 and fixes this._super call within normalize method. #4228

Merged
merged 1 commit into from
Mar 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions addon/serializers/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ var camelize = Ember.String.camelize;

/**
Normally, applications will use the `RESTSerializer` by implementing
the `normalize` method and individual normalizations under
`normalizeHash`.
the `normalize` method.

This allows you to do whatever kind of munging you need, and is
especially useful if your server is inconsistent and you need to
Expand All @@ -29,7 +28,7 @@ var camelize = Ember.String.camelize;
There are also a number of hooks that you might find useful to define
across-the-board rules for your payload. These rules will be useful
if your server is consistent, or if you're building an adapter for
an infrastructure service, like Parse, and want to encode service
an infrastructure service, like Firebase, and want to encode service
conventions.

For example, if all of your keys are underscored and all-caps, but
Expand Down Expand Up @@ -124,10 +123,8 @@ var RESTSerializer = JSONSerializer.extend({
* With `App.Comment`, `"comments"` and `{ id: 2, body: "Rails is unagi" }`

You can use this method, for example, to normalize underscored keys to camelized
or other general-purpose normalizations.

If you want to do normalizations specific to some part of the payload, you
can specify those under `normalizeHash`.
or other general-purpose normalizations. You will only need to implement
`normalize` and manipulate the payload as desired.

For example, if the `IDs` under `"comments"` are provided as `_id` instead of
`id`, you can specify how to normalize just the comments:
Expand All @@ -136,18 +133,20 @@ var RESTSerializer = JSONSerializer.extend({
import DS from 'ember-data';

export default DS.RESTSerializer.extend({
normalizeHash: {
comments: function(hash) {
normalize(model, hash, prop) {
if (prop === 'comments') {
hash.id = hash._id;
delete hash._id;
return hash;
delete hash._id;
}

return this._super(...arguments);
}
});
```

The key under `normalizeHash` is just the original key that was in the original
payload.
On each call to the `normalize` method, the third parameter (`prop`) is always
one of the keys that were in the original payload or in the result of another
normalization as `normalizeResponse`.

@method normalize
@param {DS.Model} modelClass
Expand All @@ -159,7 +158,7 @@ var RESTSerializer = JSONSerializer.extend({
if (this.normalizeHash && this.normalizeHash[prop]) {
this.normalizeHash[prop](resourceHash);
}
return this._super(modelClass, resourceHash, prop);
return this._super(modelClass, resourceHash);
},

/**
Expand Down