Skip to content

Commit

Permalink
Add method argument to key serialization docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dremora committed Apr 30, 2015
1 parent 6d47319 commit 59055e1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
34 changes: 22 additions & 12 deletions packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ export default Serializer.extend({
var payloadKey = this._getMappedKey(key);

if (payloadKey === key && this.keyForAttribute) {
payloadKey = this.keyForAttribute(key);
payloadKey = this.keyForAttribute(key, 'serialize');
}

json[payloadKey] = value;
Expand Down Expand Up @@ -671,7 +671,7 @@ export default Serializer.extend({
serializePolymorphicType: function(snapshot, json, relationship) {
var key = relationship.key,
belongsTo = snapshot.belongsTo(key);
key = this.keyForAttribute ? this.keyForAttribute(key) : key;
key = this.keyForAttribute ? this.keyForAttribute(key, "serialize") : key;
if (Ember.isNone(belongsTo)) {
json[key + "_type"] = null;
Expand Down Expand Up @@ -1025,49 +1025,59 @@ export default Serializer.extend({
},

/**
`keyForAttribute` can be used to define rules for how to convert an
attribute name in your model to a key in your JSON.
`keyForAttribute` can be used to define rules for how to perform a conversion
between an attribute name in your model a a key in your JSON.
Example
```javascript
App.ApplicationSerializer = DS.RESTSerializer.extend({
keyForAttribute: function(attr) {
return Ember.String.underscore(attr).toUpperCase();
keyForAttribute: function(attr, method) {
if (method === 'serialize') {
return Ember.String.underscore(attr).toUpperCase();
} else {
return Ember.String.camelize(attr.toLowerCase());
}
}
});
```
@method keyForAttribute
@param {String} key
@param {String} method
@return {String} normalized key
*/
keyForAttribute: function(key) {
keyForAttribute: function(key, method) {
return key;
},

/**
`keyForRelationship` can be used to define a custom key when
serializing relationship properties. By default `JSONSerializer`
does not provide an implementation of this method.
serializing and deserializing relationship properties. By default
`JSONSerializer` does not provide an implementation of this method.
Example
```javascript
App.PostSerializer = DS.JSONSerializer.extend({
keyForRelationship: function(key, relationship) {
return 'rel_' + Ember.String.underscore(key);
keyForRelationship: function(key, relationship, method) {
if (method === 'serialize') {
return 'rel_' + Ember.String.underscore(key);
} else {
return Ember.String.camelize(key.split('rel_')[1]);
}
}
});
```
@method keyForRelationship
@param {String} key
@param {String} relationship typeClass
@param {String} method
@return {String} normalized key
*/

keyForRelationship: function(key, typeClass) {
keyForRelationship: function(key, typeClass, method) {
return key;
},

Expand Down
8 changes: 6 additions & 2 deletions packages/ember-data/lib/serializers/rest-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ function coerceId(id) {
```js
App.ApplicationSerializer = DS.RESTSerializer.extend({
keyForAttribute: function(attr) {
return Ember.String.underscore(attr).toUpperCase();
keyForAttribute: function(attr, method) {
if (method === 'serialize') {
return Ember.String.underscore(attr).toUpperCase();
} else {
return Ember.String.camelize(attr.toLowerCase());
}
}
});
```
Expand Down

0 comments on commit 59055e1

Please sign in to comment.