Skip to content

Commit

Permalink
Use the correct modelClass and serializer to extract a polymorphic ty…
Browse files Browse the repository at this point in the history
…pe when normalizing async relationships.

Closes emberjs#3547
  • Loading branch information
bmac committed Jul 23, 2015
1 parent 3930e45 commit 7f8e1fe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/ember-data/lib/serializers/rest-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,16 @@ var RESTSerializer = JSONSerializer.extend({
```
*/
if (isPrimary && Ember.typeOf(value) !== 'array') {
let {data, included} = this.normalize(primaryModelClass, value, prop);
var serializer, modelClass;
// Support polymorphic records in async relationships
if (value.type && store._hasModelFor(this.modelNameFromPayloadKey(value.type))) {
serializer = store.serializerFor(value.type);
modelClass = store.modelFor(value.type);
} else {
serializer = this;
modelClass = primaryModelClass;
}
let {data, included} = serializer.normalize(modelClass, value, prop);
documentHash.data = data;
if (included) {
documentHash.included.push(...included);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module("integration/serializer/rest - RESTSerializer", {
superVillain: DS.belongsTo('super-villain', { async: false }),
name: DS.attr('string')
});
YellowMinion = EvilMinion.extend();
YellowMinion = EvilMinion.extend({
eyes: DS.attr('number')
});
DoomsdayDevice = DS.Model.extend({
name: DS.attr('string'),
evilMinion: DS.belongsTo('evil-minion', { polymorphic: true, async: true })
Expand Down Expand Up @@ -461,6 +463,42 @@ test('serializeIntoHash uses payloadKeyFromModelName to normalize the payload ro
});
});

test('normalizeResponse with async polymorphic', function() {
env.registry.register('serializer:application', DS.RESTSerializer.extend({
isNewSerializerAPI: true
}));
var store = env.store;
env.adapter.findRecord = () => {
return {
doomsdayDevices: [{
id: 1,
name: "DeathRay",
links: {
evilMinion: '/doomsday-device/1/evil-minion'
}
}]
};
};

env.adapter.findBelongsTo = () => {
return {
evilMinion: {
id: 1,
type: 'yellowMinion',
name: 'Alex',
eyes: 3
}
};
};
run(function() {
store.findRecord('doomsday-device', 1).then((deathRay) => {
return deathRay.get('evilMinion');
}).then((evilMinion) => {
equal(evilMinion.get('eyes'), 3);
});
});
});

test("normalizeResponse can load secondary records of the same type without affecting the query count", function() {
var jsonHash = {
comments: [{ id: "1", body: "Parent Comment", root: true, children: [2, 3] }],
Expand Down

0 comments on commit 7f8e1fe

Please sign in to comment.