Skip to content

Commit

Permalink
[BUGFIX] Set IDs on Record Data when mutating DS Model (#6775) (#6780)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorT authored Nov 21, 2019
1 parent 9492196 commit db6967f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/-ember-data/tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,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.');
});

Expand Down
7 changes: 7 additions & 0 deletions packages/record-data/addon/-private/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,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];
Expand Down
4 changes: 4 additions & 0 deletions packages/store/addon/-private/system/model/internal-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,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) {
Expand Down
3 changes: 3 additions & 0 deletions packages/store/addon/-private/ts-interfaces/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ export default interface RecordData {
isDeletionCommitted?(): boolean;

setIsDeleted?(isDeleted: boolean): void;

// Private and experimental
__setId?(id: string): void;
}

0 comments on commit db6967f

Please sign in to comment.