Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions packages/ember-data/lib/serializers/json_serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ export default Ember.Object.extend({
});
```

You can also remove attributes by setting the `serialize` key to
false in your mapping object.

Example

```javascript
App.PersonSerializer = DS.JSONSerializer.extend({
attrs: {
admin: {serialize: false},
occupation: {key: 'career'}
}
});
```

When serialized:

```javascript
{
"career": "magician"
}
```

Note that the `admin` is now not included in the payload.

@property attrs
@type {Object}
*/
Expand Down Expand Up @@ -436,6 +460,11 @@ export default Ember.Object.extend({
value = transform.serialize(value);
}

// If attrs.key.serialize is false, do not include the value in the
// response to the server at all.
if (attrs && attrs[key] && attrs[key].serialize === false) {
return;
}
// if provided, use the mapping provided by `attrs` in
// the serializer
if (attrs && attrs[key]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@ test('Serializer should respect the attrs hash when serializing records', functi
equal(payload.title_payload_key, "Rails is omakase");
});

test('Serializer respects `serialize: false` on the attrs hash', function(){
expect(2);
env.container.register("serializer:post", DS.JSONSerializer.extend({
attrs: {
title: {serialize: false}
}
}));

post = env.store.createRecord("post", { title: "Rails is omakase"});

var payload = env.container.lookup("serializer:post").serialize(post);

ok(!payload.hasOwnProperty('title'), "Does not add the key to instance");
ok(!payload.hasOwnProperty('[object Object]'),"Does not add some random key like [object Object]");
});

test("Serializer should respect the primaryKey attribute when extracting records", function() {
env.container.register('serializer:post', DS.JSONSerializer.extend({
primaryKey: '_ID_'
Expand Down