Skip to content

Commit

Permalink
Properly invalidate async hasMany if data updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Sep 3, 2013
1 parent 6742ac6 commit ce00457
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/ember-data/lib/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ DS.Model = Ember.Object.extend(Ember.Evented, {
setupData: function(data) {
this._data = data;

var relationships = this._relationships;

this.eachRelationship(function(name, rel) {
if (rel.options.async) { relationships[name] = null; }
});

if (data) { this.pushedData(); }

this.suspendRelationshipObservers(function() {
Expand Down
1 change: 1 addition & 0 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ DS.Store = Ember.Object.extend(DS._Mappable, {

this.fetchMany(unloadedRecords, owner, resolver);
} else {
if (resolver) { resolver.resolve(); }
manyArray.set('isLoaded', true);
Ember.run.once(manyArray, 'trigger', 'didLoad');
}
Expand Down
41 changes: 41 additions & 0 deletions packages/ember-data/tests/integration/records/reload_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,44 @@ test("When a record is loaded a second time, isLoaded stays true", function() {
equal(get(this, 'isLoaded'), true, "The person is still loaded after change");
}
});

test("When a record is reloaded, its async hasMany relationships still work", function() {
env.container.register('model:person', DS.Model.extend({
name: DS.attr(),
tags: DS.hasMany('tag', { async: true })
}));

env.container.register('model:tag', DS.Model.extend({
name: DS.attr()
}));

var tags = { 1: "hipster", 2: "hair" };

env.adapter.find = function(store, type, id) {
switch (type.typeKey) {
case 'person':
return Ember.RSVP.resolve({ id: 1, name: "Tom", tags: [1, 2] });
case 'tag':
return Ember.RSVP.resolve({ id: id, name: tags[id] });
}
};

var tom;

env.store.find('person', 1).then(async(function(person) {
tom = person;
equal(person.get('name'), "Tom", "precond");

return person.get('tags');
})).then(async(function(tags) {
deepEqual(tags.mapBy('name'), [ 'hipster', 'hair' ]);

return tom.reload();
})).then(async(function(person) {
equal(person.get('name'), "Tom", "precond");

return person.get('tags');
})).then(async(function(tags) {
deepEqual(tags.mapBy('name'), [ 'hipster', 'hair' ], "The tags are still there");
}));
});

0 comments on commit ce00457

Please sign in to comment.