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

Adds some documentation for JSONApiSerializer #3531

Merged
merged 1 commit into from
Jul 27, 2015
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
120 changes: 120 additions & 0 deletions packages/ember-data/lib/serializers/json-api-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,87 @@ import { pluralize, singularize } from 'ember-inflector/lib/system/string';
var dasherize = Ember.String.dasherize;

/**
Ember Data 2.0 Serializer:

In Ember Data a Serializer is used to serialize and deserialize
records when they are transferred in and out of an external source.
This process involves normalizing property names, transforming
attribute values and serializing relationships.

`JSONAPISerializer` supports the http://jsonapi.org/ spec and is the
serializer recommended by Ember Data.

This serializer normalizes a JSON API payload that looks like:

```js

// models/player.js
import DS from "ember-data";

export default DS.Model.extend({
name: DS.attr(),
skill: DS.attr(),
gamesPlayed: DS.attr(),
club: DS.belongsTo('club')
});

// models/club.js
import DS from "ember-data";

export default DS.Model.extend({
name: DS.attr(),
location: DS.attr(),
players: DS.hasMany('player')
});
```

```js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason code samples don't render correctly unless they have an empty line above the first ```.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simaob Can you add an empty line between the code examples?


{
"data": [
{
"attributes": {
"name": "Benfica",
"location": "Portugal"
},
"id": "1",
"relationships": {
"players": {
"data": [
{
"id": "3",
"type": "players"
}
]
}
},
"type": "clubs"
}
],
"included": [
{
"attributes": {
"name": "Eusebio Silva Ferreira",
"skill": "Rocket shot",
"games-played": 431
},
"id": "3",
"relationships": {
"club": {
"data": {
"id": "1",
"type": "clubs"
}
}
},
"type": "players"
}
]
}
```

to the format that the Ember Data store expects.

@class JSONAPISerializer
@namespace DS
@extends DS.JSONSerializer
Expand Down Expand Up @@ -202,6 +283,26 @@ export default JSONSerializer.extend({
},

/**
`keyForAttribute` can be used to define rules for how to convert an
attribute name in your model to a key in your JSON.
By default `JSONAPISerializer` follows the format used on the examples of
http://jsonapi.org/format and uses dashes as the word separator in the JSON
attribute keys.

This behaviour can be easily customized by extending this method.

Example

```app/serializers/application.js
import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
keyForAttribute: function(attr, method) {
return Ember.String.dasherize(attr).toUpperCase();
}
});
```

@method keyForAttribute
@param {String} key
@param {String} method
Expand All @@ -212,6 +313,25 @@ export default JSONSerializer.extend({
},

/**
`keyForRelationship` can be used to define a custom key when
serializing and deserializing relationship properties.
By default `JSONAPISerializer` follows the format used on the examples of
http://jsonapi.org/format and uses dashes as word separators in
relationship properties.

This behaviour can be easily customized by extending this method.

Example

```app/serializers/post.js
import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
keyForRelationship: function(key, relationship, method) {
return Ember.String.underscore(key);
}
});
```
@method keyForRelationship
@param {String} key
@param {String} typeClass
Expand Down