diff --git a/addon/-private/system/model/internal-model.js b/addon/-private/system/model/internal-model.js index 06b283de3e1..aeaacb122f4 100644 --- a/addon/-private/system/model/internal-model.js +++ b/addon/-private/system/model/internal-model.js @@ -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) { @@ -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(); }); } diff --git a/tests/integration/relationships/has-many-test.js b/tests/integration/relationships/has-many-test.js index 5794fc3c364..01a24b158a7 100644 --- a/tests/integration/relationships/has-many-test.js +++ b/tests/integration/relationships/has-many-test.js @@ -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; @@ -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;