Skip to content

Commit

Permalink
set id
Browse files Browse the repository at this point in the history
  • Loading branch information
igorT committed Nov 20, 2019
1 parent cee9f87 commit 683ac50
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/-ember-data/tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,27 @@ module('unit/model - Model', function(hooks) {
assert.equal(person.get('id'), 'john', 'new id should be correctly set.');
});

test('ID mutation (complicated) igor', async function(assert) {
let idChange = 0;
const OddPerson = Model.extend({
name: DSattr('string'),
idComputed: computed('id', function() {}),
idDidChange: observer('id', () => idChange++),
});
this.owner.register('model:odd-person', OddPerson);

let person = store.createRecord('odd-person');
person.get('idComputed');
assert.equal(idChange, 0);

assert.equal(person.get('id'), null, 'initial created model id should be null');
assert.equal(idChange, 0);
person.set('id', 'john');
let recordData = recordDataFor(person);
assert.equal(recordData.getResourceIdentifier().id, 'john', 'new id should be correctly set.');
assert.equal(recordData.id, 'john', 'new id should be correctly set.');
});

test('an ID of 0 is allowed', async function(assert) {
store.push({
data: {
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 @@ -1359,6 +1359,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 683ac50

Please sign in to comment.