So every once in a while you need many-to-ony and one-to-many relationships, in angular you can fix this problem with the following fix: (see forwardRef):
externaltarget.ts
@Model({dashedSingularName : 'external-target', dashedPluralName: 'external-targets'})
export class ExternalTarget extends Resource {
@Field()
public id: string;
@Field()
public percentage: number;
@Field()
public startDate: Date;
@Field()
public endDate: Date;
@ToOne(forwardRef(() => ExternalTarget))
public employee: ToOneRelation<ExternalTarget, Employee>;
public static async get(): Promise<ExternalTarget[]> {
const params = new HttpParams().set('include', 'employee');
return this.fetch();
}
}
employee.ts
Model()
export class Employee extends Resource {
@Field()
public id: number;
@Field()
public code: string;
@Field()
public firstName: string;
@Field()
public middleName: string;
@Field()
public lastName: string;
@ToMany(ExternalTarget, 'external-targets')
public externalTargets: ToManyRelation<Employee, ExternalTarget>;
public fullName: string;
public static async get(): Promise<Employee[]> {
const params = new HttpParams().set('include', 'external-targets');
return this.fetch({ params: params });
}
onInit() {
this.fullName = this.firstName + ' ' + this.middleName + ' ' + this.lastName;
}
}
Now the circular dependency is ok, however when using json:api spec the attribute employee on external target is not an attribute but a relationship when you encounter it nested in a response.
In short: when a resource is being nested in a response in jsonapi, dont parse the relationships, they are not needed at that point. only go one level deep.
So every once in a while you need
many-to-onyandone-to-manyrelationships, in angular you can fix this problem with the following fix: (seeforwardRef):externaltarget.ts
employee.ts
Now the circular dependency is ok, however when using json:api spec the attribute
employeeon external target is not an attribute but a relationship when you encounter it nested in a response.In short: when a resource is being nested in a response in jsonapi, dont parse the relationships, they are not needed at that point. only go one level deep.