Skip to content

Commit

Permalink
embedded records mixin should use the correct serialization key when …
Browse files Browse the repository at this point in the history
…deserialize configuration is set

When extending the `DS.EmbeddedRecordsMixin`, if you configure a relationship as `{ serialize: 'id', deserialize: 'records' }`, it won't actually pay attention to the `serialize: 'id'` option, but use the `deserialize: 'records'` option.
  • Loading branch information
agrobbin committed Mar 18, 2015
1 parent a9a2af0 commit 1bbf895
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ var ActiveModelSerializer = RESTSerializer.extend({
});
}
} else {
payloadKey = this.keyForRelationship(key, relationship.kind);
payloadKey = this.keyForRelationship(key, relationship.kind, "deserialize");
if (!hash.hasOwnProperty(payloadKey)) { return; }
payload = hash[payloadKey];
}
Expand Down
12 changes: 6 additions & 6 deletions packages/ember-data/lib/serializers/embedded-records-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
return extractEmbeddedRecords(this, this.store, type, normalizedHash);
},

keyForRelationship: function(key, type) {
if (this.hasDeserializeRecordsOption(key)) {
keyForRelationship: function(key, type, method) {
if ((method === 'serialize' && this.hasSerializeRecordsOption(key)) || (method === 'deserialize' && this.hasDeserializeRecordsOption(key))) {
return this.keyForAttribute(key);
} else {
return this._super(key, type) || key;
return this._super(key, type, method) || key;
}
},

Expand Down Expand Up @@ -191,7 +191,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
var embeddedSnapshot = snapshot.belongsTo(attr);
var key;
if (includeIds) {
key = this.keyForRelationship(attr, relationship.kind);
key = this.keyForRelationship(attr, relationship.kind, 'serialize');
if (!embeddedSnapshot) {
json[key] = null;
} else {
Expand Down Expand Up @@ -299,7 +299,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
var includeRecords = this.hasSerializeRecordsOption(attr);
var key;
if (includeIds) {
key = this.keyForRelationship(attr, relationship.kind);
key = this.keyForRelationship(attr, relationship.kind, 'serialize');
json[key] = snapshot.hasMany(attr, { ids: true });
} else if (includeRecords) {
key = this.keyForAttribute(attr);
Expand Down Expand Up @@ -335,7 +335,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
if (parentRecord) {
var name = parentRecord.name;
var embeddedSerializer = this.store.serializerFor(embeddedSnapshot.type);
var parentKey = embeddedSerializer.keyForRelationship(name, parentRecord.kind);
var parentKey = embeddedSerializer.keyForRelationship(name, parentRecord.kind, 'deserialize');
if (parentKey) {
delete json[parentKey];
}
Expand Down
8 changes: 4 additions & 4 deletions packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export default Serializer.extend({

if (this.keyForRelationship) {
type.eachRelationship(function(key, relationship) {
payloadKey = this.keyForRelationship(key, relationship.kind);
payloadKey = this.keyForRelationship(key, relationship.kind, 'deserialize');
if (key === payloadKey) { return; }
if (!hash.hasOwnProperty(payloadKey)) { return; }

Expand Down Expand Up @@ -572,7 +572,7 @@ export default Serializer.extend({
var belongsTo = snapshot.belongsTo(key);
key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo") : key;
key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo", "serialize") : key;
json[key] = Ember.isNone(belongsTo) ? belongsTo : belongsTo.record.toJSON();
}
Expand All @@ -594,7 +594,7 @@ export default Serializer.extend({
// the serializer
var payloadKey = this._getMappedKey(key);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, "belongsTo");
payloadKey = this.keyForRelationship(key, "belongsTo", "serialize");
}

//Need to check whether the id is there for new&async records
Expand Down Expand Up @@ -644,7 +644,7 @@ export default Serializer.extend({
// the serializer
payloadKey = this._getMappedKey(key);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, "hasMany");
payloadKey = this.keyForRelationship(key, "hasMany", "serialize");
}

var relationshipType = snapshot.type.determineRelationshipType(relationship);
Expand Down
5 changes: 3 additions & 2 deletions packages/ember-data/lib/serializers/rest-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ function coerceId(id) {
```
You can also implement `keyForRelationship`, which takes the name
of the relationship as the first parameter, and the kind of
relationship (`hasMany` or `belongsTo`) as the second parameter.
of the relationship as the first parameter, the kind of
relationship (`hasMany` or `belongsTo`) as the second parameter, and
the method (`serialize` or `deserialize`) as the third parameter.
@class RESTSerializer
@namespace DS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,41 @@ test("serialize with embedded object (belongsTo relationship) supports serialize
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:id in conjunction with deserialize:records", function() {
env.registry.register('adapter:superVillain', DS.ActiveModelAdapter);
env.registry.register('serializer:superVillain', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { serialize: 'id', deserialize: 'records' }
}
}));

var serializer = env.container.lookup("serializer:superVillain");

// records with an id, persisted
var tom, json;

run(function() {
tom = env.store.createRecord(
SuperVillain,
{ firstName: "Tom", lastName: "Dale", id: "1",
secretLab: env.store.createRecord(SecretLab, { minionCapacity: 5000, vicinity: "California, USA", id: "101" }),
homePlanet: env.store.createRecord(HomePlanet, { name: "Villain League", id: "123" })
}
);
});

run(function() {
json = serializer.serialize(tom._createSnapshot());
});

deepEqual(json, {
first_name: get(tom, "firstName"),
last_name: get(tom, "lastName"),
home_planet_id: get(tom, "homePlanet").get("id"),
secret_lab_id: get(tom, "secretLab").get("id")
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:false", function() {
env.registry.register('adapter:superVillain', DS.ActiveModelAdapter);
env.registry.register('serializer:superVillain', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
Expand Down

0 comments on commit 1bbf895

Please sign in to comment.