@@ -13,7 +13,7 @@ include ../_util-fns
1313 It also makes it easier to unit test the component with a mock service.
1414
1515 Because data services are invariably asynchronous,
16- we'll finish the chapter with a promise -based version of the data service.
16+ we'll finish the chapter with a **!{_Promise}** -based version of the data service.
1717
1818p Run the #[ + liveExampleLink2('' , 'toh-4' )] for this part.
1919
@@ -64,20 +64,20 @@ code-example(language="bash").
6464 First, defining heroes is not the component's job.
6565 Second, we can't easily share that list of heroes with other components and views.
6666
67- We can refactor this hero data acquisition business to a single service that provides heroes and
67+ We can refactor this hero data acquisition business to a single service that provides heroes, and
6868 share that service with all components that need heroes.
6969
7070 ### Create the HeroService
7171 Create a file in the `app` folder called `hero.service.ts`.
7272.l-sub-section
7373 :marked
7474 We've adopted a convention in which we spell the name of a service in lowercase followed by `.service`.
75- If the service name were multi-word, we'd spell the base filename with lower dash case (AKA "kebab -case" ).
75+ If the service name were multi-word, we'd spell the base filename in lower [ dash- case](../guide/glossary.html#!#dash -case).
7676 The `SpecialSuperHeroService` would be defined in the `special-super-hero.service.ts` file.
7777:marked
7878 We name the class `HeroService` and export it for others to import.
7979
80- + makeExample('toh-4/ts/app/hero.service.1.ts' , 'empty-class' , 'hero.service.ts (exported class )' )( format ="." )
80+ + makeExample('toh-4/ts/app/hero.service.1.ts' , 'empty-class' , 'app/ hero.service.ts (starting point )' )( format ="." )
8181
8282:marked
8383 ### Injectable Services
@@ -96,7 +96,7 @@ code-example(language="bash").
9696:marked
9797 ### Getting Heroes
9898 Add a `getHeroes` method stub.
99- + makeExample('toh-4/ts/app/hero.service.1.ts' , 'getHeroes-stub' , 'hero.service.ts ( getHeroes stub)' )( format ="." )
99+ + makeExample('toh-4/ts/app/hero.service.1.ts' , 'getHeroes-stub' , 'app/ hero.service.ts (getHeroes stub)' )( format ="." )
100100:marked
101101 We're holding back on the implementation for a moment to make an important point.
102102
@@ -117,30 +117,30 @@ code-example(language="bash").
117117 Cut the `HEROES` array from `app.component.ts` and paste it to a new file in the `app` folder named `mock-heroes.ts`.
118118 We copy the `import {Hero} ...` statement as well because the heroes array uses the `Hero` class.
119119
120- + makeExample('toh-4/ts/app/mock-heroes.ts' , null , 'mock-heroes.ts (Heroes array) ' )
120+ + makeExample('toh-4/ts/app/mock-heroes.ts' , null , 'app/ mock-heroes.ts' )
121121:marked
122122 We export the `HEROES` constant so we can import it elsewhere — such as our `HeroService`.
123123
124124 Meanwhile, back in `app.component.ts` where we cut away the `HEROES` array,
125125 we leave behind an uninitialized `heroes` property:
126- + makeExample('toh-4/ts/app/app.component.1.ts' , 'heroes-prop' , 'app.component.ts (heroes property)' )( format ="." )
126+ + makeExample('toh-4/ts/app/app.component.1.ts' , 'heroes-prop' , 'app/app .component.ts (heroes property)' )( format ="." )
127127:marked
128128 ### Return Mocked Heroes
129129 Back in the `HeroService` we import the mock `HEROES` and return it from the `getHeroes` method.
130130 Our `HeroService` looks like this:
131- + makeExample('toh-4/ts/app/hero.service.1.ts' , null , 'hero.service.ts' )( format ="." )
131+ + makeExample('toh-4/ts/app/hero.service.1.ts' , null , 'app/ hero.service.ts' )( format ="." )
132132:marked
133133 ### Use the Hero Service
134134 We're ready to use the `HeroService` in other components starting with our `AppComponent`.
135135
136136 We begin, as usual, by importing the thing we want to use, the `HeroService`.
137- + makeExample ('toh-4/ts/app/app.component.ts' , 'hero-service-import' , 'app.component.ts (import HeroService) ' )
137+ + makeExcerpt ('toh-4/ts/app/app.component.ts' , 'hero-service-import' )
138138:marked
139139 Importing the service allows us to *reference* it in our code.
140140 How should the `AppComponent` acquire a runtime concrete `HeroService` instance?
141141
142142 ### Do we *new* the *HeroService*? No way!
143- We could create a new instance of the `HeroService` with " new" like this:
143+ We could create a new instance of the `HeroService` with ` new` like this:
144144+ makeExample('toh-4/ts/app/app.component.1.ts' , 'new-service' )( format ="." )
145145:marked
146146 That's a bad idea for several reasons including
@@ -150,7 +150,7 @@ code-example(language="bash").
150150 we'll have to find every place we create the service and fix it.
151151 Running around patching code is error prone and adds to the test burden.
152152
153- * We create a new service each time we use " new" .
153+ * We create a new service each time we use ` new` .
154154 What if the service should cache heroes and share that cache with others?
155155 We couldn't do that.
156156
@@ -168,11 +168,11 @@ code-example(language="bash").
168168 ### Inject the *HeroService*
169169
170170 Two lines replace the one line that created with *new*:
171- 1. we add a constructor.
172- 1. we add to the component's `providers` metadata
171+ 1. We add a constructor that also defines a private property .
172+ 1. We add to the component's `providers` metadata.
173173
174174 Here's the constructor:
175- + makeExample('toh-4/ts/app/app.component.1.ts' , 'ctor' , 'app.component.ts (constructor)' )
175+ + makeExample('toh-4/ts/app/app.component.1.ts' , 'ctor' , 'app/app .component.ts (constructor)' )
176176:marked
177177 The constructor itself does nothing. The parameter simultaneously
178178 defines a private `heroService` property and identifies it as a `HeroService` injection site.
@@ -185,14 +185,14 @@ code-example(language="bash").
185185:marked
186186 The *injector* does not know yet how to create a `HeroService`.
187187 If we ran our code now, Angular would fail with an error:
188- code-example( format ="." language = "html ") .
188+ code-example( format ="nocode " ) .
189189 EXCEPTION: No provider for HeroService! (AppComponent -> HeroService)
190190:marked
191191 We have to teach the *injector* how to make a `HeroService` by registering a `HeroService` **provider**.
192192 Do that by adding the following `providers` array property to the bottom of the component metadata
193193 in the `@Component` call.
194194
195- + makeExample ('toh-4/ts/app/app.component.1.ts' , 'providers' , 'app.component.ts (providing HeroService) ' )
195+ + makeExcerpt ('toh-4/ts/app/app.component.1.ts' , 'providers' )
196196:marked
197197 The `providers` array tells Angular to create a fresh instance of the `HeroService` when it creates a new `AppComponent`.
198198 The `AppComponent` can use that service to get heroes and so can every child component of its component tree.
@@ -205,7 +205,7 @@ a#child-component
205205+ makeExample('toh-4/ts/app/app.component.1.ts' , 'get-heroes' )( format ="." )
206206:marked
207207 We don't really need a dedicated method to wrap one line. We write it anyway:
208- + makeExample ('toh-4/ts/app/app.component.1.ts' , 'getHeroes' , 'app.component.ts (getHeroes)' ) ( format = "." )
208+ + makeExcerpt ('toh-4/ts/app/app.component.1.ts' , 'getHeroes' )
209209
210210<a id =" oninit" ></a >
211211:marked
@@ -232,18 +232,19 @@ a#child-component
232232 Learn more about lifecycle hooks in the [Lifecycle Hooks](../guide/lifecycle-hooks.html) chapter.
233233:marked
234234 Here's the essential outline for the `OnInit` interface:
235- + makeExample('toh-4/ts/app/app.component.1.ts' , 'on-init' , 'app.component.ts (OnInit protocol )' )( format ="." )
235+ + makeExample('toh-4/ts/app/app.component.1.ts' , 'on-init' , 'app/app .component.ts (ngOnInit stub )' )( format ="." )
236236:marked
237237 We write an `ngOnInit` method with our initialization logic inside and leave it to Angular to call it
238238 at the right time. In our case, we initialize by calling `getHeroes`.
239- + makeExample ('toh-4/ts/app/app.component.1.ts' , 'ng-on-init' , 'app.component.ts (OnInit protocol)' ) ( format = "." )
239+ + makeExcerpt ('toh-4/ts/app/app.component.1.ts' , 'ng-on-init' )
240240:marked
241241 Our application should be running as expected, showing a list of heroes and a hero detail view
242242 when we click on a hero name.
243243
244244 We're getting closer. But something isn't quite right.
245245
246- ## Async Services and Promises
246+ <a id="async"></a>
247+ ## Async Services and !{_Promise}s
247248 Our `HeroService` returns a list of mock heroes immediately.
248249 Its `getHeroes` signature is synchronous
249250+ makeExample('toh-4/ts/app/app.component.1.ts' , 'get-heroes' )( format ="." )
@@ -257,34 +258,34 @@ a#child-component
257258
258259 We'll have to use some kind of asynchronous technique and that will change the signature of our `getHeroes` method.
259260
260- We'll use *promises *.
261+ We'll use *!{_Promise}s *.
261262
262- ### The Hero Service makes a promise
263+ ### The Hero Service makes a !{_Promise}
263264
264- A **promise ** is ... well it's a promise to call us back later when the results are ready.
265+ A **!{_Promise} ** is ... well it's a promise to call us back later when the results are ready.
265266 We ask an asynchronous service to do some work and give it a callback function.
266267 It does that work (somewhere) and eventually it calls our function with the results of the work or an error.
267268.l-sub-section
268269 :marked
269270 We are simplifying. Learn about ES2015 Promises [here](http://exploringjs.com/es6/ch_promises.html) and elsewhere on the web.
270271:marked
271- Update the `HeroService` with this promise -returning `getHeroes` method:
272- + makeExample('toh-4/ts/app/hero.service.ts' , 'get-heroes' , 'hero.service.ts (getHeroes )' )( format ="." )
272+ Update the `HeroService` with this !{_Promise} -returning `getHeroes` method:
273+ + makeExample('toh-4/ts/app/hero.service.ts' , 'get-heroes' , 'app/ hero.service.ts (excerpt )' )( format ="." )
273274:marked
274275 We're still mocking the data. We're simulating the behavior of an ultra-fast, zero-latency server,
275- by returning an **immediately resolved promise ** with our mock heroes as the result.
276+ by returning an **immediately resolved !{_Promise} ** with our mock heroes as the result.
276277
277- ### Act on the Promise
278+ ### Act on the !{_Promise}
278279 Returning to the `AppComponent` and its `getHeroes` method, we see that it still looks like this:
279- + makeExample('toh-4/ts/app/app.component.1.ts' , 'getHeroes' , 'app.component.ts (getHeroes - old)' )( format ="." )
280+ + makeExample('toh-4/ts/app/app.component.1.ts' , 'getHeroes' , 'app/app .component.ts (getHeroes - old)' )( format ="." )
280281:marked
281- As a result of our change to `HeroService`, we're now setting `this.heroes` to a promise rather than an array of heroes.
282+ As a result of our change to `HeroService`, we're now setting `this.heroes` to a !{_Promise} rather than an array of heroes.
282283
283- We have to change our implementation to *act on the promise when it resolves*.
284- When the promise resolves successfully, *then* we will have heroes to display.
284+ We have to change our implementation to *act on the !{_Promise} when it resolves*.
285+ When the !{_Promise} resolves successfully, *then* we will have heroes to display.
285286
286- We pass our callback function as an argument to the promise 's **then** method:
287- + makeExample('toh-4/ts/app/app.component.ts' , 'get-heroes' , 'app.component.ts (getHeroes - revised)' )( format ="." )
287+ We pass our callback function as an argument to the !{_Promise} 's **then** method:
288+ + makeExample('toh-4/ts/app/app.component.ts' , 'get-heroes' , 'app/app .component.ts (getHeroes - revised)' )( format ="." )
288289.l-sub-section
289290 :marked
290291 The [ES2015 arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
@@ -316,6 +317,8 @@ a#child-component
316317 .file typings ...
317318 .file index.html
318319 .file package.json
320+ .file styles.css
321+ .file systemjs.config.js
319322 .file tsconfig.json
320323 .file typings.json
321324:marked
@@ -334,11 +337,11 @@ a#child-component
334337 ## The Road We’ve Travelled
335338 Let’s take stock of what we’ve built.
336339
337- * We created a service class that can be shared by many components
338- * We used the `ngOnInit` Lifecycle Hook to get our heroes when our `AppComponent` activates
339- * We defined our `HeroService` as a provider for our `AppComponent`
340- * We created mock hero data and imported them into our service
341- * We designed our service to return a promise and our component to get our data from the promise
340+ * We created a service class that can be shared by many components.
341+ * We used the `ngOnInit` Lifecycle Hook to get our heroes when our `AppComponent` activates.
342+ * We defined our `HeroService` as a provider for our `AppComponent`.
343+ * We created mock hero data and imported them into our service.
344+ * We designed our service to return a !{_Promise} and our component to get our data from the !{_Promise}.
342345
343346p Run the #[ + liveExampleLink2('' , 'toh-4' )] for this part.
344347:marked
@@ -357,10 +360,10 @@ p Run the #[+liveExampleLink2('', 'toh-4')] for this part.
357360 We can simulate a slow connection.
358361
359362 Import the `Hero` symbol and add the following `getHeroesSlowly` method to the `HeroService`
360- + makeExample('toh-4/ts/app/hero.service.ts' , 'get-heroes-slowly' , 'hero.service.ts (getHeroesSlowly)' )( format ="." )
363+ + makeExample('toh-4/ts/app/hero.service.ts' , 'get-heroes-slowly' , 'app/ hero.service.ts (getHeroesSlowly)' )( format ="." )
361364:marked
362- Like `getHeroes`, it also returns a promise .
363- But this promise waits 2 seconds before resolving the promise with mock heroes.
365+ Like `getHeroes`, it also returns a !{_Promise} .
366+ But this !{_Promise} waits 2 seconds before resolving the !{_Promise} with mock heroes.
364367
365368 Back in the `AppComponent`, replace `heroService.getHeroes` with `heroService.getHeroesSlowly`
366369 and see how the app behaves.
0 commit comments