Maybe it's better explained with an example.
I have the following class:
export class Foo {
bar: string;
doSomething() {
// whatever
}
}
And the following provider:
@Injectable()
export class FooProvider {
constructor(private http: HttpClient) {
}
getFoos(): Observable<Foo[]> {
return this.http.get<Foo[]>('/foos')
}
}
I was expecting that when subscribing to the Observable
, that the data would be an actual array of Foo
objects. But to my surprise, the objects inside the array are simple javascript objects, with only data, and no methods (actually when trying to execute doSomething()
in any of them a is not a function
errors appears.
The only way I've been able to do so is by mapping the observable like this:
@Injectable()
export class FooProvider {
constructor(private http: HttpClient) {
}
getFoos(): Observable<Foo[]> {
return this.http.get<Foo[]>('/foos')
.map(foos => {
foos.map(foo => {
let f = new Foo();
Object.assign(f, foo);
return f;
};
});
}
}
The thing is, that I don't want to have to do so in every method of every provider. Seeing that the code to do the conversion is quite simple, I'm guessing that I'm missing something, but I'm unable to find so in the docs.
Could anyone help me with this?