SUMMARY: I'm encountering an issue where I am fetching Object instances instead of Org instances from my data in Angular 2. Is there a way to retrieve Org objects directly or is this the expected behavior?
DETAILS: In my Angular 2 project, I have modified some tutorial code and here is a snippet from my org.service.ts:
getOrgs(): Promise<Org[]> {
return this.http
.get(this.orgUrl)
.toPromise()
.then(response => response.json().data as Org[])
.catch(this.handleError);
}
getOrg(id: number): Promise<Org> {
return this.getOrgs()
.then((orgs: Org[]) : Org => orgs.find(org => org.id === id));
}
This is part of my resolver:
resolve(route: ActivatedRouteSnapshot): Promise<Org>|boolean {
let id = this.authService.user.orgId;
return this.orgService.getOrg(id).then((org: Org) : Org|boolean => {
if(org) { // <-- Breakpoint here
return org;
} else {
this.router.navigate(['/provider/home']);
return false;
}
});
}
I'm using an in-memory service to fetch my data:
export class InMemoryDataService {
createDb() {
let org1 = new Org();
org1.id = 12;
org1.donor = true;
org1.name = 'Great Actors Theater';
let org2 = new Org();
org2.id = 19;
org2.donor = false;
org2.name = 'Sunnyside Group Home';
let org: Org[] = [];
org.push(org1);
org.push(org2);
[SNIP]
Below is my Org class definition:
export class Org {
id: number;
donor: boolean;
name: string = "";
deepCopy(): Org { ... }
equals(other: Org): boolean { ... }
}
At a breakpoint, I noticed that the fetched object resembles an Org instance with fields (id, donor, name) but lacks class methods such as equals(). It also doesn't recognize "org instanceof Org" or "typeof org" returns "object."
I would like to work with actual Org instances with class methods rather than relying on duck-typing. Is there a better approach to achieve this?
Thank you, Jerome.