I have found “just a data model with some helper functions” to be a generally bad fit for the JavaScript world. I have had better luck with smart providers and dumb data, so I would do something like this:
export interface Customer {
id: string;
name: string;
}
export interface Purchase {
id: string;
customerId: string;
productId: string;
quantity: number;
}
@Injectable()
export class Provider {
constructor(private _http: Http) {}
customerById(id: string): Observable<Customer> {
return this._http.get('/api/customer/' + id).map(rsp => rsp.json());
}
purchasesByCustomer(id: string): Observable<Purchase[]> {
return this._http.get('/api/purchases?customer=' + id).map(rsp => rsp.json().purchases);
}
}
EDIT: I don’t know if it’s still a problem, but due to previous security concerns about naked JSON arrays, I always make my APIs return objects with a single array member instead of a raw array. Edited purchasesByCustomer()
to reflect that.