Skip to content

Commit

Permalink
[BUGFIX] Quick Hack: lazyRelationships should not emit change events …
Browse files Browse the repository at this point in the history
…on first access

This is to confirm this is the only issue we have seen upgrading. We should clean this up more, initial access to this collection should likely take a much simpler code-path.
  • Loading branch information
stefanpenner committed Nov 11, 2016
1 parent 1ca87a2 commit 520df2f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 5 additions & 2 deletions addon/-private/system/relationships/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ export const HasManyMixin = Ember.Mixin.create({
//We need to notifyPropertyChange in the adding case because we need to make sure
//we fetch the newly added record in case it is unloaded
//TODO(Igor): Consider whether we could do this only if the record state is unloaded

//Goes away once hasMany is double promisified
this.notifyPropertyChange(key);
//
let cache = Ember.meta(this).readableCache();
if (typeof cache === 'object' && cache !== null && cache[key] !== undefined) {
this.notifyPropertyChange(key);
}
}
});
17 changes: 16 additions & 1 deletion tests/unit/model/relationships/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,9 +770,13 @@ test("DS.hasMany is async by default", function(assert) {
});

test("DS.ManyArray is lazy", function(assert) {
let peopleDidChange = 0;
let Tag = DS.Model.extend({
name: DS.attr('string'),
people: DS.hasMany('person')
people: DS.hasMany('person'),
peopleDidChange: Ember.observer('people', function() {
peopleDidChange++;
})
});

let Person = DS.Model.extend({
Expand All @@ -787,9 +791,20 @@ test("DS.ManyArray is lazy", function(assert) {
let hasManyRelationship = tag.hasMany('people').hasManyRelationship;
assert.ok(!hasManyRelationship._manyArray);
run(function() {
assert.equal(peopleDidChange, 0, 'expect people hasMany to not emit a change event (before access)');
tag.get('people');
assert.equal(peopleDidChange, 0, 'expect people hasMany to not emit a change event (sync after access)');
});
assert.equal(peopleDidChange, 0, 'expect people hasMany to not emit a change event (after access, but after the current run loop)');
assert.ok(hasManyRelationship._manyArray instanceof DS.ManyArray);

let person = Ember.run(() => env.store.createRecord('person'));

Ember.run(() => {
assert.equal(peopleDidChange, 0, 'expect people hasMany to not emit a change event (before access)');
tag.get('people').addObject(person);
assert.equal(peopleDidChange, 1, 'expect people hasMany to have changed exactly once');
});
});

testInDebug("throws assertion if of not set with an array", function(assert) {
Expand Down

0 comments on commit 520df2f

Please sign in to comment.