Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export abstract class Model
*/
public query(): Builder<this, PluralResponse<this>>
{
return this.constructor.query();
return this.getConstructor().query();
}

/**
Expand Down Expand Up @@ -161,7 +161,7 @@ export abstract class Model
{
let attributes = {};
for (let key in this.attributes.toArray()) {
if ((this as Model).constructor.readOnlyAttributes.indexOf(key) == -1) {
if (this.getConstructor().readOnlyAttributes.indexOf(key) == -1) {
attributes[key] = this.attributes.get(key);
}
}
Expand All @@ -177,7 +177,7 @@ export abstract class Model

let payload = {
data: {
type: (this as Model).constructor.effectiveJsonApiType,
type: this.getConstructor().effectiveJsonApiType,
attributes,
relationships
}
Expand All @@ -190,7 +190,7 @@ export abstract class Model

private serializeRelatedModel(model: Model): any {
return {
type: model.constructor.effectiveJsonApiType,
type: this.getConstructorOf(model).effectiveJsonApiType,
id: model.id
};
}
Expand All @@ -214,16 +214,16 @@ export abstract class Model
}

let payload = this.serialize();
return (this as Model).constructor.effectiveHttpClient
return this.getConstructor().effectiveHttpClient
.patch(
(this as Model).constructor.getJsonApiUrl()+'/'+this.id,
this.getConstructor().getJsonApiUrl()+'/'+this.id,
payload
)
.then(
(response: HttpClientResponse) => {
const idFromJson: string | undefined = response.getData().data.id;
this.setApiId(idFromJson);
return new SaveResponse(response, Object.getPrototypeOf(this).constructor, response.getData());
return new SaveResponse(response, this.getConstructor(), response.getData());
},
(response: AxiosError) => {
throw response;
Expand All @@ -234,16 +234,16 @@ export abstract class Model
public create(): Promise<SaveResponse<this>>
{
let payload = this.serialize();
return (this as Model).constructor.effectiveHttpClient
return this.getConstructor().effectiveHttpClient
.post(
(this as Model).constructor.getJsonApiUrl(),
this.getConstructor().getJsonApiUrl(),
payload
)
.then(
(response: HttpClientResponse) => {
const idFromJson: string | undefined = response.getData().data.id;
this.setApiId(idFromJson);
return new SaveResponse(response, Object.getPrototypeOf(this).constructor, response.getData());
return new SaveResponse(response, this.getConstructor(), response.getData());
},
function (response: AxiosError) {
throw response;
Expand All @@ -256,8 +256,8 @@ export abstract class Model
if (!this.hasId) {
throw new Error('Cannot delete a model with no ID.');
}
return (this as Model).constructor.effectiveHttpClient
.delete((this as Model).constructor.getJsonApiUrl()+'/'+this.id)
return this.getConstructor().effectiveHttpClient
.delete(this.getConstructor().getJsonApiUrl()+'/'+this.id)
.then(function () {});
}

Expand All @@ -271,7 +271,7 @@ export abstract class Model
*/
public fresh(): Promise<this | null | undefined>
{
let model = <this> (new (<any> this.constructor));
let model = <this> (new (<any> this.getConstructor()));
let builder = model
.query()
.with(this.getRelationsKeys());
Expand Down Expand Up @@ -373,23 +373,23 @@ export abstract class Model
* @deprecated Use the static method with the same name instead
*/
public getJsonApiType(): string {
return (this as Model).constructor.effectiveJsonApiType;
return this.getConstructor().effectiveJsonApiType;
}

/**
* @deprecated Use the static property {@link jsonApiBaseUrl} or
* {@link effectiveJsonApiBaseUrl}
*/
public getJsonApiBaseUrl(): string {
return (this as Model).constructor.effectiveJsonApiBaseUrl;
return this.getConstructor().effectiveJsonApiBaseUrl;
}

/**
* @deprecated Use the static {@link httpClient} to get the one that is
* configured, and {@link effectiveHttpClient} to get the one that is
*/
public getHttpClient(): HttpClient {
return (this as Model).constructor.effectiveHttpClient
return this.getConstructor().effectiveHttpClient
}

public populateFromResource(resource: Resource): void
Expand Down Expand Up @@ -468,7 +468,7 @@ export abstract class Model

private isDateAttribute(attributeName: string): boolean
{
return (this as Model).constructor.dates.hasOwnProperty(attributeName);
return this.getConstructor().dates.hasOwnProperty(attributeName);
}

protected setAttribute(attributeName: string, value: any): void
Expand All @@ -477,7 +477,7 @@ export abstract class Model
if (!Date.parse(value)) {
throw new Error(`${value} cannot be cast to type Date`);
}
value = (<any> Model.getDateFormatter()).parseDate(value, (this as Model).constructor.dates[attributeName]);
value = (<any> Model.getDateFormatter()).parseDate(value, this.getConstructor().dates[attributeName]);
}

this.attributes.set(attributeName, value);
Expand Down Expand Up @@ -534,4 +534,12 @@ export abstract class Model
&& this.id !== null
&& this.id !== '';
}

private getConstructor() {
return this.getConstructorOf(this);
}

private getConstructorOf(model: Model): typeof Model {
return Object.getPrototypeOf(model).constructor;
}
}