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 22, 2016
1 parent 70ea8c0 commit 07acf97
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
21 changes: 16 additions & 5 deletions addon/serializers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -1050,11 +1050,7 @@ var JSONSerializer = Serializer.extend({
var json = {};

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

if (id) {
json[get(this, 'primaryKey')] = id;
}
this.serializeId(snapshot, json);
}

snapshot.eachAttribute((key, attribute) => {
Expand All @@ -1072,6 +1068,21 @@ var JSONSerializer = Serializer.extend({
return json;
},

/**
SerializeId can be used to customize how 'Id' is seriaziled
@method serializeId
@param {DS.Snapshot} snapshot
@param {Object} json
*/

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

/**
You can use this method to customize how a serialized record is added to the complete
JSON hash to be sent to the server. By default the JSON Serializer does not namespace
Expand Down
14 changes: 14 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,20 @@ test("serialize includes id when includeId is true", function(assert) {
});
});

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

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

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

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

0 comments on commit 07acf97

Please sign in to comment.