Skip to content

Commit

Permalink
Fix handling of relationship loading to use dataKey.
Browse files Browse the repository at this point in the history
Data for a relationship should be looked up on the dataKey, and not the
plain property key. Previously, loads on a model with relationships
would not update the embedded models when the property key didn't
match the data key.
  • Loading branch information
brettburley committed Jan 16, 2016
1 parent f1ef88a commit 8f4b78c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/ember-model/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Ember.Model = Ember.Object.extend(Ember.Evented, {
this._reloadHasManys();

// eagerly load embedded data
var relationships = this.constructor._relationships || [], meta = Ember.meta(this), relationshipKey, relationship, relationshipMeta, relationshipData, relationshipType;
var relationships = this.constructor._relationships || [], meta = Ember.meta(this), relationshipKey, relationship, relationshipMeta, dataKey, relationshipData, relationshipType;
for (var i = 0, l = relationships.length; i < l; i++) {
relationshipKey = relationships[i];
relationship = (meta.descs || this)[relationshipKey];
Expand All @@ -140,7 +140,8 @@ Ember.Model = Ember.Object.extend(Ember.Evented, {
relationshipType = Ember.get(Ember.lookup, relationshipType) || this.container.lookupFactory('model:'+ relationshipType);
}

relationshipData = data[relationshipKey];
dataKey = this.dataKey(relationshipKey)
relationshipData = data[dataKey];
if (relationshipData) {
relationshipType.load(relationshipData);
}
Expand Down
41 changes: 41 additions & 0 deletions packages/ember-model/tests/has_many/embedded_objects_load_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,44 @@ test("loading embedded data into a parent with deleted children deletes the chil
equal(post.get('comments.length'), 1);
equal(post.get('comments.firstObject.body'), 'new');
});

test("loading embedded data into a parent updates the child records with key option", function() {
expect(2);

var json = {
id: 1,
replies: [
{id: 1, body: 'new'}
]
};

var Comment = Ember.Model.extend({
id: attr(),
body: attr()
});

Comment.adapter = {
find: function(record, id) {
record.load(id, {body: 'old'});
}
};

var Post = Ember.Model.extend({
id: attr(),
comments: Ember.hasMany(Comment, {key: 'replies', embedded: true})
});

Post.adapter = {
find: function(record, id) {
record.load(id, {replies: []});
}
};

var comment = Comment.find(1);
equal(comment.get('body'), 'old');

var post = Post.find(1);
post.load(1, json);

equal(comment.get('body'), 'new');
});

0 comments on commit 8f4b78c

Please sign in to comment.