From f11491ed8a256c727cc245b74bea2a204445445f Mon Sep 17 00:00:00 2001 From: Igor Terzic Date: Wed, 20 Nov 2019 12:40:38 -0800 Subject: [PATCH] [BUGFIX] Set IDs on Record Data when mutating DS Model (#6775) --- packages/-ember-data/tests/unit/model-test.js | 7 +++++++ .../store/addon/-private/system/model/internal-model.ts | 4 ++++ packages/store/addon/-private/system/model/record-data.ts | 7 +++++++ packages/store/addon/-private/ts-interfaces/record-data.ts | 3 +++ 4 files changed, 21 insertions(+) diff --git a/packages/-ember-data/tests/unit/model-test.js b/packages/-ember-data/tests/unit/model-test.js index 7d815a771f2..8185dfcf457 100644 --- a/packages/-ember-data/tests/unit/model-test.js +++ b/packages/-ember-data/tests/unit/model-test.js @@ -373,6 +373,13 @@ module('unit/model - Model', function(hooks) { assert.equal(idChange, 0); person._internalModel.setId('john'); assert.equal(idChange, 1); + let recordData = recordDataFor(person); + assert.equal( + recordData.getResourceIdentifier().id, + 'john', + 'new id should be set on the identifier on record data.' + ); + assert.equal(recordData.id, 'john', 'new id should be correctly set on the record data itself.'); assert.equal(person.get('id'), 'john', 'new id should be correctly set.'); }); diff --git a/packages/store/addon/-private/system/model/internal-model.ts b/packages/store/addon/-private/system/model/internal-model.ts index 25e8647f875..8a9b83d9f8a 100644 --- a/packages/store/addon/-private/system/model/internal-model.ts +++ b/packages/store/addon/-private/system/model/internal-model.ts @@ -1350,6 +1350,10 @@ export default class InternalModel { if (didChange && id !== null) { this.store.setRecordId(this.modelName, id, this.clientId); + // internal set of ID to get it to RecordData from DS.Model + if (this._recordData.__setId) { + this._recordData.__setId(id); + } } if (didChange && this.hasRecord) { diff --git a/packages/store/addon/-private/system/model/record-data.ts b/packages/store/addon/-private/system/model/record-data.ts index 2b3b77bdd77..78dfd946df5 100644 --- a/packages/store/addon/-private/system/model/record-data.ts +++ b/packages/store/addon/-private/system/model/record-data.ts @@ -429,6 +429,13 @@ export default class RecordDataDefault implements RelationshipRecordData { } } + // internal set coming from the model + __setId(id: string) { + if (this.id !== id) { + this.id = id; + } + } + getAttr(key: string): string { if (key in this._attributes) { return this._attributes[key]; diff --git a/packages/store/addon/-private/ts-interfaces/record-data.ts b/packages/store/addon/-private/ts-interfaces/record-data.ts index 6b62d53adff..c287e44e5af 100644 --- a/packages/store/addon/-private/ts-interfaces/record-data.ts +++ b/packages/store/addon/-private/ts-interfaces/record-data.ts @@ -65,4 +65,7 @@ export default interface RecordData { isDeletionCommitted?(): boolean; setIsDeleted?(isDeleted: boolean): void; + + // Private and experimental + __setId?(id: string): void; }