Skip to content

Commit

Permalink
[FEATURE serializeId] add serializeId to json-serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Romanior committed Oct 23, 2016
1 parent 70ea8c0 commit 87cc598
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
16 changes: 16 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,19 @@ entry in `config/features.json`.
tom.resetAttribute('lastName') // { firstName: 'Tom', lastName: 'Dale' }
tom.get('hasDirtyAttributes') // false
```

- `ds-serialize-id` [#4620](https://github.com/emberjs/data/pull/4620)

Adds a `serializeId` method to JSONSerializer.

```js
// app/serializers/application.js
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
serializeId(snapshot, json, primaryKey) {
var id = snapshot.id;
json[primaryKey] = parseInt(id, 10);
}
});
```
48 changes: 44 additions & 4 deletions addon/serializers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -1050,10 +1050,13 @@ var JSONSerializer = Serializer.extend({
var json = {};

if (options && options.includeId) {
var id = snapshot.id;

if (id) {
json[get(this, 'primaryKey')] = id;
if (isEnabled('ds-serialize-id')) {
this.serializeId(snapshot, json, get(this, 'primaryKey'));
} else {
var id = snapshot.id;
if (id) {
json[get(this, 'primaryKey')] = id;
}
}
}

Expand Down Expand Up @@ -1535,4 +1538,41 @@ if (isEnabled("ds-payload-type-hooks")) {

}

if (isEnabled("ds-serialize-id")) {

JSONSerializer.reopen({

/**
serializeId can be used to customize how id is serialized
For example, your server may expect integer datatype of id
By default the snapshot's id {String} is set on the json hash via json[primaryKey] = snapshot.id.
```app/serializers/application.js
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
serializeId(snapshot, json, primaryKey) {
var id = snapshot.id;
json[primaryKey] = parseInt(id, 10);
}
});
```
@method serializeId
@public
@param {DS.Snapshot} snapshot
@param {Object} json
@param {String} primaryKey
*/
serializeId(snapshot, json, primaryKey) {
var id = snapshot.id;

if (id) {
json[primaryKey] = id;
}
}
});
}

export default JSONSerializer;
3 changes: 2 additions & 1 deletion config/features.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"ds-overhaul-references": null,
"ds-payload-type-hooks": null,
"ds-check-should-serialize-relationships": null,
"ds-reset-attribute": null
"ds-reset-attribute": null,
"ds-serialize-id": null
}
60 changes: 60 additions & 0 deletions tests/integration/serializers/json-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,66 @@ test("serialize includes id when includeId is true", function(assert) {
});
});

if (isEnabled("ds-serialize-id")) {
test("serializeId", function(assert) {
run(function() {
post = env.store.createRecord('post');
post.set('id', 'test');
});
var json = {};

env.serializer.serializeId(post._createSnapshot(), json, 'id');

assert.deepEqual(json, {
id: 'test'
});
});

test("modified serializeId is called from serialize", function(assert) {
env.registry.register('serializer:post', DS.JSONSerializer.extend({
serializeId(snapshot, json, primaryKey) {
var id = snapshot.id;
json[primaryKey] = id.toUpperCase();
}
}));

run(function() {
post = env.store.createRecord('post', { title: 'Rails is omakase' });
post.set('id', 'test');
});
var json = {};

json = env.store.serializerFor("post").serialize(post._createSnapshot(), { includeId: true });

assert.deepEqual(json, {
id: 'TEST',
title: 'Rails is omakase',
comments: []
});
});

test("serializeId respects `primaryKey` serializer property", function(assert) {
env.registry.register('serializer:post', DS.JSONSerializer.extend({
primaryKey: '_ID_'
}));

run(function() {
post = env.store.createRecord('post', { title: 'Rails is omakase' });
post.set('id', 'test');
});
var json = {};

json = env.store.serializerFor("post").serialize(post._createSnapshot(), { includeId: true });

assert.deepEqual(json, {
_ID_: 'test',
title: 'Rails is omakase',
comments: []
});
});

}

test("serializeAttribute", function(assert) {
run(function() {
post = env.store.createRecord('post', { title: "Rails is omakase" });
Expand Down

0 comments on commit 87cc598

Please sign in to comment.