Skip to content

Commit

Permalink
Merge pull request #5079 from emberjs/release-fix-unload-after-destroy
Browse files Browse the repository at this point in the history
[BACKPORT 2 release] Release fix unload after destroy
  • Loading branch information
stefanpenner authored Jul 21, 2017
2 parents a8908dc + 0d4b65b commit 9ddac1a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
15 changes: 11 additions & 4 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,12 @@ export default class InternalModel {
}
}
});
Object.keys(this._implicitRelationships).forEach((key) => {
let rel = this._implicitRelationships[key];

let implicitRelationships = this._implicitRelationships;
this.__implicitRelationships = null;

Object.keys(implicitRelationships).forEach((key) => {
let rel = implicitRelationships[key];

rel.removeCompletelyFromInverse();
if (isNew === true) {
Expand All @@ -919,8 +923,11 @@ export default class InternalModel {
rel.removeInverseRelationships();
}
});
Object.keys(this._implicitRelationships).forEach((key) => {
this._implicitRelationships[key].removeInverseRelationships();

let implicitRelationships = this._implicitRelationships;
this.__implicitRelationships = null;
Object.keys(implicitRelationships).forEach((key) => {
implicitRelationships[key].removeInverseRelationships();
});
}

Expand Down
66 changes: 64 additions & 2 deletions tests/integration/relationships/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2134,10 +2134,8 @@ test("Relationship.clear removes all records correctly", function(assert) {
var comments = Ember.A(env.store.peekAll('comment'));
assert.deepEqual(comments.mapBy('post'), [null, null, null]);
});

});


test('unloading a record with associated records does not prevent the store from tearing down', function(assert) {
var post;

Expand Down Expand Up @@ -2755,6 +2753,70 @@ test("PromiseArray proxies createRecord to its ManyArray before the hasMany is l
});
});

test("deleteRecord + unloadRecord fun", function(assert) {
User.reopen({
posts: DS.hasMany('posts', { inverse: null })
});

run(() => {
env.store.push({
data: [
{
type: 'user',
id: 'user-1',
attributes: {
name: 'Adolfo Builes'
},
relationships: {
posts: {
data: [
{ type: 'post', id: 'post-1' },
{ type: 'post', id: 'post-2' },
{ type: 'post', id: 'post-3' },
{ type: 'post', id: 'post-4' },
{ type: 'post', id: 'post-5' }
]
}
}
},
{ type: 'post', id: 'post-1' },
{ type: 'post', id: 'post-2' },
{ type: 'post', id: 'post-3' },
{ type: 'post', id: 'post-4' },
{ type: 'post', id: 'post-5' }
]
});

let user = env.store.peekRecord('user', 'user-1');
let posts = user.get('posts');

env.store.adapterFor('post').deleteRecord = function() {
// just acknowledge all deletes, but with a noop
return null;
};

assert.deepEqual(posts.map(x => x.get('id')), ['post-1', 'post-2', 'post-3', 'post-4', 'post-5']);

return run(() => {
return env.store.peekRecord('post', 'post-2').destroyRecord().then(record => {
return env.store.unloadRecord(record);
});
}).then(() => {
assert.deepEqual(posts.map(x => x.get('id')), ['post-1', 'post-3', 'post-4', 'post-5']);
return env.store.peekRecord('post', 'post-3').destroyRecord().then(record => {
return env.store.unloadRecord(record);
});
}).then(() => {
assert.deepEqual(posts.map(x => x.get('id')), ['post-1', 'post-4', 'post-5']);
return env.store.peekRecord('post', 'post-4').destroyRecord().then(record => {
return env.store.unloadRecord(record);
});
}).then(() => {
assert.deepEqual(posts.map(x => x.get('id')), ['post-1', 'post-5']);
});
});
});

test("unloading and reloading a record with hasMany relationship - #3084", function(assert) {
var user;
var message;
Expand Down

0 comments on commit 9ddac1a

Please sign in to comment.