Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX #4649 #4648] #4643

Merged
merged 1 commit into from
Nov 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions addon/-private/system/many-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {
this.relationship = this.relationship || null;

this.currentState = Ember.A([]);
this.flushCanonical();
this.flushCanonical(false);
},

objectAt(index) {
Expand All @@ -141,7 +141,7 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {
return this.currentState[index].getRecord();
},

flushCanonical() {
flushCanonical(isInitialized = true) {
let toSet = this.canonicalState;

//a hack for not removing new records
Expand All @@ -161,8 +161,11 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {
}
this.currentState = toSet;
this.arrayContentDidChange(0, oldLength, this.length);
//TODO Figure out to notify only on additions and maybe only if unloaded
this.relationship.notifyHasManyChanged();

if (isInitialized) {
//TODO Figure out to notify only on additions and maybe only if unloaded
this.relationship.notifyHasManyChanged();
}
},

internalReplace(idx, amt, objects) {
Expand All @@ -173,10 +176,6 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {
this.currentState.splice.apply(this.currentState, [idx, amt].concat(objects));
this.set('length', this.currentState.length);
this.arrayContentDidChange(idx, amt, objects.length);
if (objects) {
//TODO(Igor) probably needed only for unloaded records
this.relationship.notifyHasManyChanged();
}
},

//TODO(Igor) optimize
Expand Down
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