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

[FEATURE ds-serialize-id] add serializeId to json-serializer #4620

Merged
merged 1 commit into from
Oct 23, 2016
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
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