diff --git a/packages/ember-data/lib/serializers/json-serializer.js b/packages/ember-data/lib/serializers/json-serializer.js index 4d7020c6bc1..ae5f3d7d29b 100644 --- a/packages/ember-data/lib/serializers/json-serializer.js +++ b/packages/ember-data/lib/serializers/json-serializer.js @@ -758,6 +758,41 @@ export default Serializer.extend({ return !attrs || !attrs[key] || attrs[key].serialize !== false; }, + /** + When attrs.key.serialize is set to true then + it takes priority over the other checks and the related + attribute/relationship will be serialized + + @method _mustSerialize + @private + @param {String} key + @return {boolean} true if the key must be serialized + */ + _mustSerialize: function(key) { + var attrs = get(this, 'attrs'); + + return attrs && attrs[key] && attrs[key].serialize === true; + }, + + /** + Check if the given hasMany relationship should be serialized + + @method _shouldSerializeHasMany + @private + @param {DS.Snapshot} snapshot + @param {String} key + @param {String} relationshipType + @return {boolean} true if the hasMany relationship should be serialized + */ + _shouldSerializeHasMany: function (snapshot, key, relationship) { + var relationshipType = snapshot.type.determineRelationshipType(relationship, this.store); + if (this._mustSerialize(key)) { + return true; + } + return this._canSerialize(key) && (relationshipType === 'manyToNone' || relationshipType === 'manyToMany'); + }, + + // SERIALIZE /** Called when a record is saved in order to convert the @@ -1095,7 +1130,7 @@ export default Serializer.extend({ serializeHasMany: function(snapshot, json, relationship) { var key = relationship.key; - if (this._canSerialize(key)) { + if (this._shouldSerializeHasMany(snapshot, key, relationship)) { var payloadKey; // if provided, use the mapping provided by `attrs` in @@ -1104,13 +1139,8 @@ export default Serializer.extend({ if (payloadKey === key && this.keyForRelationship) { payloadKey = this.keyForRelationship(key, "hasMany", "serialize"); } - - var relationshipType = snapshot.type.determineRelationshipType(relationship, this.store); - - if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany') { - json[payloadKey] = snapshot.hasMany(key, { ids: true }); - // TODO support for polymorphic manyToNone and manyToMany relationships - } + json[payloadKey] = snapshot.hasMany(key, { ids: true }); + // TODO support for polymorphic manyToNone and manyToMany relationships } }, diff --git a/packages/ember-data/tests/integration/serializers/json-serializer-test.js b/packages/ember-data/tests/integration/serializers/json-serializer-test.js index be5c04ee057..02c9d3d5f5d 100644 --- a/packages/ember-data/tests/integration/serializers/json-serializer-test.js +++ b/packages/ember-data/tests/integration/serializers/json-serializer-test.js @@ -318,6 +318,86 @@ test('Serializer respects `serialize: false` on the attrs hash for a `belongsTo` ok(!payload.hasOwnProperty(serializedProperty), "Does not add the key to instance"); }); +test('Serializer respects `serialize: false` on the attrs hash for a `hasMany` property', function() { + expect(1); + env.registry.register("serializer:post", DS.JSONSerializer.extend({ + attrs: { + comments: { serialize: false } + } + })); + + run(function() { + post = env.store.createRecord('post', { title: "Rails is omakase" }); + comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post }); + }); + + var serializer = env.container.lookup("serializer:post"); + var serializedProperty = serializer.keyForRelationship('comments', 'hasMany'); + + var payload = serializer.serialize(post._createSnapshot()); + ok(!payload.hasOwnProperty(serializedProperty), "Does not add the key to instance"); +}); + +test('Serializer respects `serialize: false` on the attrs hash for a `belongsTo` property', function() { + expect(1); + env.registry.register("serializer:comment", DS.JSONSerializer.extend({ + attrs: { + post: { serialize: false } + } + })); + + run(function() { + post = env.store.createRecord('post', { title: "Rails is omakase" }); + comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post }); + }); + + var serializer = env.container.lookup("serializer:comment"); + var serializedProperty = serializer.keyForRelationship('post', 'belongsTo'); + + var payload = serializer.serialize(comment._createSnapshot()); + ok(!payload.hasOwnProperty(serializedProperty), "Does not add the key to instance"); +}); + +test('Serializer respects `serialize: true` on the attrs hash for a `hasMany` property', function() { + expect(1); + env.registry.register("serializer:post", DS.JSONSerializer.extend({ + attrs: { + comments: { serialize: true } + } + })); + + run(function() { + post = env.store.createRecord('post', { title: "Rails is omakase" }); + comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post }); + }); + + var serializer = env.container.lookup("serializer:post"); + var serializedProperty = serializer.keyForRelationship('comments', 'hasMany'); + + var payload = serializer.serialize(post._createSnapshot()); + ok(payload.hasOwnProperty(serializedProperty), "Add the key to instance"); +}); + +test('Serializer respects `serialize: true` on the attrs hash for a `belongsTo` property', function() { + expect(1); + env.registry.register("serializer:comment", DS.JSONSerializer.extend({ + attrs: { + post: { serialize: true } + } + })); + + run(function() { + post = env.store.createRecord('post', { title: "Rails is omakase" }); + comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post }); + }); + + var serializer = env.container.lookup("serializer:comment"); + var serializedProperty = serializer.keyForRelationship('post', 'belongsTo'); + + var payload = serializer.serialize(comment._createSnapshot()); + ok(payload.hasOwnProperty(serializedProperty), "Add the key to instance"); +}); + test("Serializer should merge attrs from superclasses", function() { expect(4); Post.reopen({