Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 94d432d

Browse files
committed
docs(toh-6/ts): minor updates
1 parent a9b6eaa commit 94d432d

File tree

3 files changed

+71
-53
lines changed

3 files changed

+71
-53
lines changed

public/docs/_examples/toh-6/ts/app/dashboard.component.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// #docplaster
2-
// #docregion
3-
// #docregion hero-search-component
1+
// #docregion , search
42
import { Component, OnInit } from '@angular/core';
53
import { Router } from '@angular/router';
64

@@ -14,9 +12,8 @@ import { HeroSearchComponent } from './hero-search.component';
1412
styleUrls: ['app/dashboard.component.css'],
1513
directives: [HeroSearchComponent]
1614
})
17-
// #enddocregion hero-search-component
15+
// #enddocregion search
1816
export class DashboardComponent implements OnInit {
19-
2017
heroes: Hero[] = [];
2118

2219
constructor(

public/docs/_examples/toh-6/ts/app/hero-search.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class HeroSearchComponent implements OnInit {
1818
heroes: Observable<Hero[]>;
1919
// #enddocregion search
2020
// #docregion searchTerms
21-
searchTerms = new Subject<string>();
21+
private searchTerms = new Subject<string>();
2222
// #enddocregion searchTerms
2323

2424
constructor(

public/docs/ts/latest/tutorial/toh-pt6.jade

+68-47
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ block dont-be-distracted-by-backend-subst
111111

112112
Look at our current `HeroService` implementation
113113

114-
+makeExample('toh-4/ts/app/hero.service.ts', 'get-heroes', 'app/hero.service.ts (old getHeroes)')(format=".")
114+
+makeExcerpt('toh-4/ts/app/hero.service.ts (old getHeroes)', 'get-heroes')
115115

116116
:marked
117117
We returned a !{_Promise} resolved with mock heroes.
@@ -135,21 +135,21 @@ block get-heroes-details
135135

136136
For *now* we get back on familiar ground by immediately by
137137
converting that `Observable` to a `Promise` using the `toPromise` operator.
138-
+makeExample('toh-6/ts/app/hero.service.ts', 'to-promise')(format=".")
138+
+makeExcerpt('app/hero.service.ts', 'to-promise')
139139
:marked
140140
Unfortunately, the Angular `Observable` doesn't have a `toPromise` operator ... not out of the box.
141141
The Angular `Observable` is a bare-bones implementation.
142142

143143
There are scores of operators like `toPromise` that extend `Observable` with useful capabilities.
144144
If we want those capabilities, we have to add the operators ourselves.
145145
That's as easy as importing them from the RxJS library like this:
146-
+makeExample('toh-6/ts/app/hero.service.ts', 'rxjs')(format=".")
146+
+makeExcerpt('app/hero.service.ts', 'rxjs')
147147

148148
:marked
149149
### Extracting the data in the *then* callback
150150
In the *promise*'s `then` callback we call the `json` method of the http `Response` to extract the
151151
data within the response.
152-
+makeExample('toh-6/ts/app/hero.service.ts', 'to-data')(format=".")
152+
+makeExcerpt('app/hero.service.ts', 'to-data')
153153

154154
:marked
155155
That response JSON has a single `data` property.
@@ -209,7 +209,7 @@ block get-heroes-details
209209

210210
### Put
211211

212-
Put will be used to update an individual hero. Its structure is very similar to Post requests. The only difference is that we have to change the url slightly by appending the id of the hero we want to update.
212+
Put will be used to update an individual hero. Its structure is very similar to Post requests. The only difference is that we have to change the URL slightly by appending the id of the hero we want to update.
213213

214214
+makeExcerpt('app/hero.service.ts', 'put')
215215

@@ -259,7 +259,7 @@ block hero-detail-comp-updates
259259
+makeExcerpt('app/hero-detail.component.ts', 'ngOnInit')
260260

261261
:marked
262-
In order to differentiate between add and edit we are adding a check to see if an id is passed in the url. If the id is absent we bind `HeroDetailComponent` to an empty `Hero` object. In either case, any edits made through the UI will be bound back to the same `hero` property.
262+
In order to differentiate between add and edit we are adding a check to see if an id is passed in the URL. If the id is absent we bind `HeroDetailComponent` to an empty `Hero` object. In either case, any edits made through the UI will be bound back to the same `hero` property.
263263

264264
:marked
265265
Add a save method to `HeroDetailComponent` and call the corresponding save method in `HeroesService`.
@@ -357,12 +357,13 @@ block review
357357
### Let's see it
358358
Here are the fruits of labor in action:
359359
figure.image-display
360-
img(src='/resources/images/devguide/toh/toh-http.anim.gif' alt="Heroes List Editting w/ HTTP")
360+
img(src='/resources/images/devguide/toh/toh-http.anim.gif' alt="Heroes List Editing w/ HTTP")
361361

362-
block observables-section
363362
:marked
364-
## Observables
363+
## !{_Observable}s
365364

365+
block observables-section-intro
366+
:marked
366367
Each `Http` method returns an `Observable` of HTTP `Response` objects.
367368

368369
Our `HeroService` converts that `Observable` into a `Promise` and returns the promise to the caller.
@@ -384,59 +385,78 @@ block observables-section
384385
A single result in the form of a promise is easy for the calling component to consume
385386
and it helps that promises are widely understood by JavaScript programmers.
386387

388+
:marked
387389
But requests aren't always "one and done". We may start one request,
388-
then cancel it, and make a different request ... before the server has responded to the first request.
389-
Such a _request-cancel-new-request_ sequence is difficult to implement with *promises*.
390-
It's easy with *observables* as we'll see.
390+
then cancel it, and make a different request before the server has responded to the first request.
391+
Such a _request-cancel-new-request_ sequence is difficult to implement with *!{_Promise}s*.
392+
It's easy with *!{_Observable}s* as we'll see.
391393

392394
### Search-by-name
393395
We're going to add a *hero search* feature to the Tour of Heroes.
394-
As the user types a name into a search box, we'll make repeated http requests for heroes filtered by that name.
396+
As the user types a name into a search box, we'll make repeated HTTP requests for heroes filtered by that name.
395397

396398
We start by creating `HeroSearchService` that sends search queries to our server's web api.
397399

398-
+makeExample('toh-6/ts/app/hero-search.service.ts', null, 'app/hero-search.service.ts')(format=".")
400+
+makeExample('app/hero-search.service.ts')
399401

400402
:marked
401-
The `http.get` call in `HeroSearchService` is similar to the `http.get` call in the `HeroService`.
402-
The notable difference: we no longer call `toPromise`.
403-
We simply return the *observable* instead.
403+
The `!{_priv}http.get()` call in `HeroSearchService` is similar to the one
404+
in the `HeroService`, although the URL now has a query string.
405+
<span if-docs="ts">Another notable difference: we no longer call `toPromise`,
406+
we simply return the *observable* instead.</span>
404407

405408
### HeroSearchComponent
409+
406410
Let's create a new `HeroSearchComponent` that calls this new `HeroSearchService`.
407411

408-
The component template is simple - just a textbox and a list of matching search results.
409-
+makeExample('toh-6/ts/app/hero-search.component.html', null,'hero-search.component.html')
412+
The component template is simple &mdash; just a text box and a list of matching search results.
413+
414+
+makeExample('app/hero-search.component.html')
415+
410416
:marked
411-
As the user types in the search box, a *keyup* event binding calls the component's `search` with the new search box value.
417+
As the user types in the search box, a *keyup* event binding calls the component's `search` method with the new search box value.
412418

413419
The `*ngFor` repeats *hero* objects from the component's `heroes` property. No surprise there.
414420

415-
But, as we'll soon see, the `heroes` property returns an `Observable` of heroes, not an array of heroes.
416-
The `*ngFor` can't do anything with an observable until we flow it through the `AsyncPipe` (`heroes | async`).
417-
The `AsyncPipe` subscribes to the observable and produces the array of heroes to `*ngFor`.
421+
But, as we'll soon see, the `heroes` property is now !{_an} *!{_Observable}* of hero !{_array}s, rather than just a hero !{_array}.
422+
The `*ngFor` can't do anything with !{_an} `!{_Observable}` until we flow it through the `async` pipe (`AsyncPipe`).
423+
The `async` pipe subscribes to the `!{_Observable}` and produces the !{_array} of heroes to `*ngFor`.
418424

419425
Time to create the `HeroSearchComponent` class and metadata.
420-
+makeExample('toh-6/ts/app/hero-search.component.ts', null,'hero-search.component.ts')
426+
427+
+makeExample('app/hero-search.component.ts')
428+
421429
:marked
422-
Focus on the `searchTerms`.
423-
+makeExample('toh-6/ts/app/hero-search.component.ts', 'searchTerms')(format=".")
430+
#### Search terms
431+
432+
Let's focus on the `!{_priv}searchTerms`:
433+
434+
+makeExcerpt('app/hero-search.component.ts', 'searchTerms')
435+
436+
block search-criteria-intro
424437
:marked
425-
A `Subject` is a producer of an _observable_ event stream.
426-
This `searchTerms` produces an `Observable` of strings, the filter criteria for the name search.
438+
A `Subject` is a producer of an _observable_ event stream;
439+
`searchTerms` produces an `Observable` of strings, the filter criteria for the name search.
427440

428441
Each call to `search` puts a new string into this subject's _observable_ stream by calling `next`.
429442

430-
A `Subject` is also an `Observable`.
431-
We're going to access that `Observable` and turn the stream
432-
of strings into a stream of `Hero[]` arrays, the `heroes` property.
443+
:marked
444+
<a id="ngoninit"></a>
445+
#### Initialize the _**heroes**_ property (_**ngOnInit**_)
446+
447+
<span if-docs="ts">A `Subject` is also an `Observable`.</span>
448+
We're going to turn the stream
449+
of search terms into a stream of `Hero` !{_array}s and assign the result to the `heroes` property.
450+
451+
+makeExcerpt('app/hero-search.component.ts', 'search')
433452

434-
+makeExample('toh-6/ts/app/hero-search.component.ts', 'search')(format=".")
435453
:marked
436-
If we passed every user keystroke directly to the `HeroSearchService`, we'd unleash a storm of http requests.
454+
If we passed every user keystroke directly to the `HeroSearchService`, we'd unleash a storm of HTTP requests.
437455
Bad idea. We don't want to tax our server resources and burn through our cellular network data plan.
438456

439-
Fortunately we can chain `Observable` operators to the string `Observable` that reduce the request flow.
457+
block observable-transformers
458+
:marked
459+
Fortunately, we can chain `Observable` operators to the string `Observable` that reduce the request flow.
440460
We'll make fewer calls to the `HeroSearchService` and still get timely results. Here's how:
441461

442462
* `debounceTime(300)` waits until the flow of new string events pauses for 300 milliseconds
@@ -486,11 +506,12 @@ block observables-section
486506
We take a different approach in this example.
487507
We combine all of the RxJS `Observable` extensions that _our entire app_ requires into a single RxJS imports file.
488508

489-
+makeExample('toh-6/ts/app/rxjs-extensions.ts', null, 'app/rxjs-extensions.ts')(format=".")
509+
+makeExample('app/rxjs-extensions.ts')
510+
490511
:marked
491512
We load them all at once by importing `rxjs-extensions` in `AppComponent`.
492513

493-
+makeExample('toh-6/ts/app/app.component.ts', 'rxjs-extensions', 'app/app.component.ts')(format=".")
514+
+makeExcerpt('app/app.component.ts', 'rxjs-extensions')
494515

495516
:marked
496517
### Add the search component to the dashboard
@@ -500,12 +521,12 @@ block observables-section
500521
+makeExample('app/dashboard.component.html')
501522

502523
:marked
503-
And finally, we import the `HeroSearchComponent` and add it to the `directives` array.
524+
And finally, we import the `HeroSearchComponent` and add it to the `directives` !{_array}.
504525

505-
+makeExcerpt('app/dashboard.component.ts', 'hero-search-component')
526+
+makeExcerpt('app/dashboard.component.ts', 'search')
506527

507528
:marked
508-
Run the app again, go to the *Dashboard*, and enter some text in the search box below the hero tiles.
529+
Run the app again, go to the *Dashboard*, and enter some text in the search box.
509530
At some point it might look like this.
510531

511532
figure.image-display
@@ -534,9 +555,9 @@ block filetree
534555
.file hero-detail.component.css
535556
.file hero-detail.component.html
536557
.file hero-detail.component.ts
537-
.file hero-search.component.html
538-
.file hero-search.component.ts
539-
.file hero-search.service.ts
558+
.file hero-search.component.html (new)
559+
.file hero-search.component.ts (new)
560+
.file hero-search.service.ts (new)
540561
.file rxjs-operators.ts
541562
.file hero.service.ts
542563
.file heroes.component.css
@@ -559,14 +580,14 @@ block filetree
559580
## Home Stretch
560581

561582
We are at the end of our journey for now, but we have accomplished a lot.
562-
- We added the necessary dependencies to use Http in our application.
563-
- We refactored HeroService to load heroes from an API.
564-
- We extended HeroService to support post, put and delete calls.
583+
- We added the necessary dependencies to use HTTP in our application.
584+
- We refactored `HeroService` to load heroes from a web API.
585+
- We extended `HeroService` to support post, put and delete methods.
565586
- We updated our components to allow adding, editing and deleting of heroes.
566587
- We configured an in-memory web API.
567-
<li if-docs="ts"> We learned how to use Observables.</li>
588+
- We learned how to use !{_Observable}s.
568589

569-
Below is a summary of the files we changed and added.
590+
Here are the files we added or changed in this chapter.
570591

571592
block file-summary
572593
+makeTabs(

0 commit comments

Comments
 (0)