Referring to the Tour of heroes from the angular tutorial.
It is assumed that we will provide all existing heroes with the opportunity to wear a special suite, but any other hero can wear the same suite. We really do not want them to be naked. In this case, we will have a related data object that contains all the information about the available costumes. The hero himself must know what suite he wears, and where he can find his chosen one. For this case, we would create a property that contains the identifier of his chosen dress.
What would be the correct way to allow the connection between the hero and his package in angular as observed from the http request?
eg:
app/hero.ts
export class Hero { id: number; name: string; suiteId: number; }
app/suit.ts
export class Suite { id: number; name: string; material: string; color: string; }
app/in-memory-data.service.ts
import { InMemoryDbService } from 'angular-in-memory-web-api'; export class InMemoryDataService implements InMemoryDbService { createDb() { let heroes = [ {id: 11, name: 'Mr. Nice', suitId: 11}, {id: 12, name: 'Narco', suitId: 12} ]; let suits= [ {id: 11, name: 'Nice and blue', material: 'Cotton', color: 'blue'}, {id: 12, name: 'Sexy and red', material: 'Silk', color: 'red'} ]; return {heroes, suits}; } }
I know that something like the following block of code will work, but I would like to know how to resolve this โobservableโ method.
// ... getHeroesWithSuit(): Observable<Hero[]> { return this.http.get(this.heroesUrl) .map( response => response.json().data as Hero[]) .do( response => response.forEach( ( data ) => data.suite = this.suiteService .getSuit(data.suiteId) .subscribe(suite => data.suite = suite as Suite) )) .catch(this.handleError); } // ...