Skip to content

Commit

Permalink
Update record-refernce to use public store apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav0 committed Dec 12, 2019
1 parent 6fb02e9 commit b277b6b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { attr } from '@ember-data/model';
import JSONAPISerializer from '@ember-data/serializer/json-api';

type RecordIdentifier = import('@ember-data/store/-private/ts-interfaces/identifier').RecordIdentifier;
type NewRecordIdentifier = import('@ember-data/store/-private/ts-interfaces/identifier').NewRecordIdentifier;
type RecordData = import('@ember-data/store/-private/ts-interfaces/record-data').RecordData;
type JsonApiValidationError = import('@ember-data/store/-private/ts-interfaces/record-data-json-api').JsonApiValidationError;

Expand All @@ -25,10 +26,21 @@ class Person extends Model {
lastName;
}

class TestRecordIdentifier implements NewRecordIdentifier {
constructor(public id: string | null, public lid: string, public type: string) {}
}

class TestRecordData implements RecordData {
id = null;
id: string | null = '1';
clientId: string | null = 'test-record-data-1';
modelName = 'tst';

getResourceIdentifier() {
if (this.clientId !== null) {
return new TestRecordIdentifier(this.id, this.clientId, this.modelName);
}
}

commitWasRejected(recordIdentifier: RecordIdentifier, errors?: JsonApiValidationError[]): void {}

// Use correct interface once imports have been fix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { attr } from '@ember-data/model';
import JSONAPISerializer from '@ember-data/serializer/json-api';

type RecordData = import('@ember-data/store/-private/ts-interfaces/record-data').RecordData;
type NewRecordIdentifier = import('@ember-data/store/-private/ts-interfaces/identifier').NewRecordIdentifier;

class Person extends Model {
// TODO fix the typing for naked attrs
Expand All @@ -23,10 +24,21 @@ class Person extends Model {
lastName;
}

class TestRecordIdentifier implements NewRecordIdentifier {
constructor(public id: string | null, public lid: string, public type: string) {}
}

class TestRecordData implements RecordData {
id = null;
id: string | null = '1';
clientId: string | null = 'test-record-data-1';
modelName = 'tst';

getResourceIdentifier() {
if (this.clientId !== null) {
return new TestRecordIdentifier(this.id, this.clientId, this.modelName);
}
}

commitWasRejected(): void {}

// Use correct interface once imports have been fix
Expand Down
17 changes: 7 additions & 10 deletions packages/store/addon/-private/system/references/record.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import RSVP, { resolve } from 'rsvp';

import Reference, { INTERNAL_MODELS } from './reference';
import Reference from './reference';

type SingleResourceDocument = import('../../ts-interfaces/ember-data-json-api').SingleResourceDocument;
type RecordInstance = import('../../ts-interfaces/record-instance').RecordInstance;
Expand Down Expand Up @@ -123,10 +123,9 @@ export default class RecordReference extends Reference {
@method value
@return {Model} the record for this RecordReference
*/
value() {
let internalModel = INTERNAL_MODELS.get(this.recordData);
if (internalModel && internalModel.hasRecord) {
return internalModel.getRecord();
value(): RecordInstance | null {
if (this._id !== null) {
return this.store.peekRecord(this.type, this._id);
}
return null;
}
Expand Down Expand Up @@ -171,11 +170,9 @@ export default class RecordReference extends Reference {
@return {Promise<record>} the record for this RecordReference
*/
reload() {
let record = this.value();
if (record) {
return record.reload();
if (this._id !== null) {
return this.store.findRecord(this.type, this._id, { reload: true });
}

return this.load();
throw new Error(`Unable to fetch record of type ${this.type} without an id`);
}
}
2 changes: 2 additions & 0 deletions packages/store/addon/-private/ts-interfaces/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export interface ChangedAttributesHash {

export interface RecordData {
id: string | null;
clientId: string | null;
modelName: string;

getResourceIdentifier(): RecordIdentifier | undefined;
pushData(data: JsonApiResource, calculateChange?: boolean): void;
clientDidCreate(): void;
willCommit(): void;
Expand Down

0 comments on commit b277b6b

Please sign in to comment.