From 5bec58577f9505a6a3ec5455ea69e238d6e6e6f6 Mon Sep 17 00:00:00 2001 From: Ward Bell Date: Tue, 22 Dec 2015 10:25:30 -0800 Subject: [PATCH] docs(Tutorial): minor repairs to toh-4 close #602 John's comments Fix for https://github.com/johnpapa/angular2-tour-of-heroes/issues/41 --- public/docs/_examples/toh-4/ts/app/hero.service.ts | 2 +- public/docs/ts/latest/tutorial/toh-pt4.jade | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/public/docs/_examples/toh-4/ts/app/hero.service.ts b/public/docs/_examples/toh-4/ts/app/hero.service.ts index f25ceb07e6..7f98a725e3 100644 --- a/public/docs/_examples/toh-4/ts/app/hero.service.ts +++ b/public/docs/_examples/toh-4/ts/app/hero.service.ts @@ -15,7 +15,7 @@ export class HeroService { // See the "Take it slow" appendix //#docregion get-heroes-slowly getHeroesSlowly() { - return new Promise(resolve => + return new Promise(resolve => setTimeout(()=>resolve(HEROES), 2000) // 2 seconds ); } diff --git a/public/docs/ts/latest/tutorial/toh-pt4.jade b/public/docs/ts/latest/tutorial/toh-pt4.jade index 86a03f1447..8ca6729064 100644 --- a/public/docs/ts/latest/tutorial/toh-pt4.jade +++ b/public/docs/ts/latest/tutorial/toh-pt4.jade @@ -156,7 +156,7 @@ code-example(format="." language="bash"). Will we need different mocked versions under test? Not easy. - *What if ... what if ... Dude! We've got work to do!* + *What if ... what if ... Hey, we've got work to do!* We get it. Really we do. But it is so ridiculously easy to avoid these problems that there is no excuse for doing it wrong. @@ -303,6 +303,10 @@ code-example(format="." language="html"). We pass our callback function as an argument to the promise's **then** method: +makeExample('toh-4/ts/app/app.component.ts', 'get-heroes', 'app.component.ts (getHeroes - revised)')(format=".") +.l-sub-section + :marked + The [ES2015 arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) + in the callback is more succint than the equivalent function expression and gracefully handles *this*. :marked Our callback sets the component's `heroes` property to the array of heroes returned by the service. That's all there is to it! @@ -382,10 +386,8 @@ code-example(format="." language="html"). ### Appendix: Shadowing the parent's service We stated [earlier](#child-component) that if we injected the parent `AppComponent` `HeroService` - into the `HeroDetailComponent`, *we must not add a providers array* like the following - to the `HeroDetailComponent`. -+makeExample('toh-4/ts/app/app.component.1.ts', 'providers', 'No! Don\'t do this!') -:marked + into the `HeroDetailComponent`, *we must not add a providers array* to the `HeroDetailComponent` metadata. + Why? Because that tells Angular to create a new instance of the `HeroService` at the `HeroDetailComponent` level. The `HeroDetailComponent` doesn't want its *own* service instance; it wants its *parent's* service instance. Adding the `providers` array creates a new service instance that shadows the parent instance.