From 9967bdcb64d03d86c87744d94ab935f5566a9b5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Mu=C3=B1oz?= Date: Wed, 9 Aug 2017 19:39:29 -0400 Subject: [PATCH] [BUGFIX release] Don't notify relationships with links during initialization --- .../relationships/state/relationship.js | 7 ++-- addon/-private/system/store.js | 2 +- .../relationships/belongs-to-test.js | 35 +++++++++++++++++++ .../relationships/has-many-test.js | 31 ++++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/addon/-private/system/relationships/state/relationship.js b/addon/-private/system/relationships/state/relationship.js index 80758a524b1..dec9644ef71 100644 --- a/addon/-private/system/relationships/state/relationship.js +++ b/addon/-private/system/relationships/state/relationship.js @@ -346,7 +346,7 @@ export default class Relationship { this.store._updateRelationshipState(this); } - updateLink(link) { + updateLink(link, initial) { heimdall.increment(updateLink); warn(`You pushed a record of type '${this.internalModel.modelName}' with a relationship '${this.key}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload.`, this.isAsync || this.hasData , { id: 'ds.store.push-link-for-sync-relationship' @@ -355,7 +355,10 @@ export default class Relationship { this.link = link; this.linkPromise = null; - this.internalModel.notifyPropertyChange(this.key); + + if (!initial) { + this.internalModel.notifyPropertyChange(this.key); + } } findLink() { diff --git a/addon/-private/system/store.js b/addon/-private/system/store.js index 000597131ab..da6905c4459 100644 --- a/addon/-private/system/store.js +++ b/addon/-private/system/store.js @@ -2881,7 +2881,7 @@ function setupRelationships(store, internalModel, data, modelNameToInverseMap) { if (relationshipRequiresNotification) { let relationshipData = data.relationships[relationshipName]; - relationships.get(relationshipName).push(relationshipData); + relationships.get(relationshipName).push(relationshipData, false); } // in debug, assert payload validity eagerly diff --git a/tests/integration/relationships/belongs-to-test.js b/tests/integration/relationships/belongs-to-test.js index 7baef8aff59..5d9e434d3f2 100644 --- a/tests/integration/relationships/belongs-to-test.js +++ b/tests/integration/relationships/belongs-to-test.js @@ -1406,3 +1406,38 @@ testInDebug("A belongsTo relationship warns if malformatted data is pushed into }); }, /expected the data for the book relationship on a to be in a JSON API format/); }); + +test("belongsTo relationship with links doesn't trigger extra change notifications - #4942", function(assert) { + Chapter.reopen({ + book: DS.belongsTo({ async: true }) + }); + + run(() => { + env.store.push({ + data: { + type: 'chapter', + id: '1', + relationships: { + book: { + data: { type: 'book', id: '1' }, + links: { related: '/chapter/1/book' } + } + } + }, + included: [{ type: 'book', id: '1' }] + }); + }); + + let chapter = env.store.peekRecord('chapter', '1'); + let count = 0; + + chapter.addObserver('book', () => { + count++; + }); + + run(() => { + chapter.get('book'); + }); + + assert.equal(count, 0); +}); diff --git a/tests/integration/relationships/has-many-test.js b/tests/integration/relationships/has-many-test.js index ee23bc09942..5386e40f899 100644 --- a/tests/integration/relationships/has-many-test.js +++ b/tests/integration/relationships/has-many-test.js @@ -3057,3 +3057,34 @@ test("deleted records should stay deleted", function(assert) { ); }); }); + +test("hasMany relationship with links doesn't trigger extra change notifications - #4942", function(assert) { + run(() => { + env.store.push({ + data: { + type: 'book', + id: '1', + relationships: { + chapters: { + data: [{ type: 'chapter', id: '1' }], + links: { related: '/book/1/chapters' } + } + } + }, + included: [{ type: 'chapter', id: '1' }] + }); + }); + + let book = env.store.peekRecord('book', '1'); + let count = 0; + + book.addObserver('chapters', () => { + count++; + }); + + run(() => { + book.get('chapters'); + }); + + assert.equal(count, 0); +});