Skip to content

Commit

Permalink
Merge pull request #5145 from emberjs/beta-backport-5144
Browse files Browse the repository at this point in the history
{backport beta} Beta backport 5144
  • Loading branch information
stefanpenner authored Aug 24, 2017
2 parents 7ae3f56 + 43b6136 commit 72faf40
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 4 deletions.
5 changes: 1 addition & 4 deletions addon/-private/system/relationships/state/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,18 @@ export default class ManyRelationship extends Relationship {
return;
}

let forCanonical = [];

for (let i = 0; i< internalModels.length; i++) {
let internalModel = internalModels[i];
if (this.canonicalMembers.has(internalModel)) {
continue;
}

forCanonical.push(internalModel);
this.canonicalMembers.add(internalModel);
this.members.add(internalModel);
this.setupInverseRelationship(internalModel);
}

this.canonicalState.splice(0, this.canonicalState.length, ...forCanonical);
this.canonicalState = this.canonicalMembers.toArray();
}

fetchLink() {
Expand Down
130 changes: 130 additions & 0 deletions tests/unit/model/relationships/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,136 @@ test('hasMany with duplicates from payload', function(assert) {
});
});

test('many2many loads both sides #5140', function(assert) {
assert.expect(3);

const Tag = DS.Model.extend({
name: DS.attr('string'),
people: DS.hasMany('person', { async: false })
});

Tag.reopenClass({
toString() {
return 'tag';
}
});

const Person = DS.Model.extend({
name: DS.attr('string'),
tags: DS.hasMany('tags', { async: false })
});

Person.reopenClass({
toString() {
return 'person';
}
});

let env = setupStore({ tag: Tag, person: Person });
let { store } = env;

run(() => {
// first we push in data with the relationship
store.push({
data: [
{
type: 'person',
id: 1,
attributes: {
name: 'David J. Hamilton'
},
relationships: {
tags: [{
data: {
type: 'tag',
id: 1
}
},
{
data: {
type: 'tag',
id: 2
}
}]
}
},
{
type: 'person',
id: 2,
attributes: {
name: 'Gerald Dempsey Posey'
},
relationships: {
tags: [{
data: {
type: 'tag',
id: 1
}
},
{
data: {
type: 'tag',
id: 2
}
}]
}
},
{
type: 'tag',
id: 1,
attributes: {
name: 'whatever'
},
relationships: {
people: {
data: [
{
type: 'person',
id: 1
},
{
type: 'person',
id: 2
}
]
}
}
},
{
type: 'tag',
id: 2,
attributes: {
name: 'nothing'
},
relationships: {
people: {
data: [
{
type: 'person',
id: 1
},
{
type: 'person',
id: 2
}
]
}
}
}
]
});
});

run(() => {
let tag = store.peekRecord('tag', 1);
assert.equal(tag.get('people.length'), 2, 'relationship does contain all data');
let person1 = store.peekRecord('person', 1);
assert.equal(person1.get('tags.length'), 2, 'relationship does contain all data');
let person2 = store.peekRecord('person', 2);
assert.equal(person2.get('tags.length'), 2, 'relationship does contain all data');
});
});

test('hasMany with explicit null works even when the inverse was set to not null', function(assert) {
assert.expect(3);

Expand Down

0 comments on commit 72faf40

Please sign in to comment.