Skip to content

Commit

Permalink
[BUGFIX] Set IDs on Record Data when mutating DS Model (#6775) (#6778)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorT authored Nov 21, 2019
1 parent b101be1 commit c5f8580
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 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 @@ -351,6 +351,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
10 changes: 8 additions & 2 deletions packages/store/addon/-private/system/model/internal-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,9 @@ export default class InternalModel {
return this._updatePromiseProxyFor('hasMany', key, { promise, content: manyArray });
} else {
assert(
`You looked up the '${key}' relationship on a '${this.type.modelName}' with id ${this.id} but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async ('hasMany({ async: true })')`,
`You looked up the '${key}' relationship on a '${this.type.modelName}' with id ${
this.id
} but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async ('hasMany({ async: true })')`,
!manyArray.anyUnloaded()
);

Expand Down Expand Up @@ -1267,7 +1269,11 @@ export default class InternalModel {
this.id = id;

if (didChange && id !== null) {
this.store.setRecordId(this.modelName, id, this.clientId as string);
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
7 changes: 7 additions & 0 deletions packages/store/addon/-private/system/model/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,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
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 @@ -60,4 +60,7 @@ export default interface RecordData {
isDeletionCommitted?(): boolean;

setIsDeleted?(isDeleted: boolean): void;

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

0 comments on commit c5f8580

Please sign in to comment.