diff --git a/FEATURES.md b/FEATURES.md index e542252d6fc..5757625abfd 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -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); + } + }); + ``` diff --git a/addon/serializers/json.js b/addon/serializers/json.js index 29ee9ad0dc5..5ab4ad7c27a 100644 --- a/addon/serializers/json.js +++ b/addon/serializers/json.js @@ -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; + } } } @@ -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; diff --git a/config/features.json b/config/features.json index b68623e34ef..4b9c28cb124 100644 --- a/config/features.json +++ b/config/features.json @@ -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 } diff --git a/tests/integration/serializers/json-serializer-test.js b/tests/integration/serializers/json-serializer-test.js index 73fc5b9bf50..00cb4d20105 100644 --- a/tests/integration/serializers/json-serializer-test.js +++ b/tests/integration/serializers/json-serializer-test.js @@ -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" });