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

Commit b6ef9c6

Browse files
committed
tweak
tweak
1 parent 7c290d5 commit b6ef9c6

File tree

4 files changed

+24
-27
lines changed

4 files changed

+24
-27
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!-- #docregion -->
22
<div id="search-component">
33
<h4>Hero Search</h4>
4-
<input id="search-box" [ngFormControl]="search" />
5-
<div *ngIf="showResult">
4+
<input #searchBox id="search-box" (keyup)="search.next(searchBox.value)" />
5+
<div>
66
<div class="search-result" (click)="gotoDetail(hero)" *ngFor="let hero of heroes">
77
{{hero.name}}
88
</div>
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// #docregion
22
import { Component, OnInit } from '@angular/core';
3-
import { Control } from '@angular/common';
43
import { Router } from '@angular/router';
4+
import { Observable } from 'rxjs/Observable';
5+
import { Subject } from 'rxjs/Subject';
56

67
import { HeroSearchService } from './hero-search.service';
78
import { Hero } from './hero';
@@ -12,9 +13,8 @@ import { Hero } from './hero';
1213
providers: [HeroSearchService]
1314
})
1415
export class HeroSearchComponent implements OnInit {
15-
search = new Control('');
16+
search = new Subject<string>();
1617
heroes: Hero[] = [];
17-
showResult = true;
1818

1919
constructor(private _heroSearchService: HeroSearchService, private _router: Router) {}
2020

@@ -25,13 +25,13 @@ export class HeroSearchComponent implements OnInit {
2525

2626
// #docregion search
2727
ngOnInit() {
28-
this.search.valueChanges
28+
this.search.asObservable()
2929
.debounceTime(300)
3030
.distinctUntilChanged()
31-
.do(value => this.showResult = value.length > 0)
32-
.filter(value => value.length > 0)
33-
.switchMap(term => this._heroSearchService.search(term))
34-
.subscribe(result => this.heroes = result, error => console.log(error));
31+
.switchMap(term => term ? this._heroSearchService.search(term)
32+
: Observable.of([]))
33+
.subscribe(result => this.heroes = result,
34+
error => console.log(error));
3535
}
3636
// #enddocregion search
3737
}

public/docs/_examples/toh-6/ts/app/rxjs-operators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ import 'rxjs/add/operator/filter';
55
import 'rxjs/add/operator/do';
66
import 'rxjs/add/operator/distinctUntilChanged';
77
import 'rxjs/add/operator/debounceTime';
8+
import 'rxjs/add/observable/of';

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,13 @@ block heroes-comp-add
351351
But the component is still responsible for updating the display.
352352
So the *delete* method removes the deleted hero from the list.
353353

354+
block review
355+
:marked
356+
### Let's see it
357+
Here are the fruits of labor in action:
358+
figure.image-display
359+
img(src='/resources/images/devguide/toh/toh-http.anim.gif' alt="Heroes List Editting w/ HTTP")
360+
354361
:marked
355362
## Observables
356363

@@ -384,7 +391,7 @@ block heroes-comp-add
384391
:marked
385392
The `HeroSearchComponent` UI is simple - just a textbox and a list of matching search results.
386393

387-
We have bound the `search` property to the textbox as an `ngFormControl` in order to track changes.
394+
To keep track of text changes in the search box we are using an RxJs `Subject`.
388395

389396
Observables are great for managing event streams. In our example we will actually be dealing with two different types of event streams:
390397

@@ -399,28 +406,24 @@ block heroes-comp-add
399406
+makeExample('toh-6/ts/app/hero-search.component.ts', 'search', 'app/hero-search.component.ts')(format=".")
400407

401408
:marked
402-
### valueChanges
403-
`ValueChanges` is an observable that represents text changes triggered by typing into the search textbox.
409+
### asObservable
410+
We start the observable chain by calling `asObservable` on our `Subject` to return an observable that represents text changes as we are typing into the search box.
404411

405412
### debounceTime(300)
406413
By specifying a debounce time of 300 ms we are telling `RxJs` that we only want to be notified of key events after intervals of 300 ms. This is a performance measure meant to prevent us from hitting the api too often with partial search queries.
407414

408415
### distinctUntilChanged
409416
`distinctUntilChanged` tells `RxJs` to only react if the value of the textbox actually changed.
410417

411-
### do
412-
`do` is an operator for triggering side effects outside the observable chain. In our case we are using it to set a flag to hide the search results if the search textbox is empty.
413-
414-
### filter
415-
By specifying a `filter` we are telling `RxJs` to only process search values that meet our filter condition.
416-
417418
### switchMap
418419
`switchMap` is where observables from key events and observables from http calls converge on a single observable.
419420

420421
Every qualifying key event will trigger an http call, so we may get into a situation where we have multiple http calls in flight.
421422

422423
Normally this could lead to results being returned out of order, but `switchMap` has built in support for ensuring that only the most recent http call will be processed. Results from prior calls will be discarded.
423424

425+
We short circuit the http call and return an observable containing an empty array if the search box is empty.
426+
424427
### subscribe
425428
`subscribe` is similar to `then` in the promise world.
426429

@@ -431,7 +434,7 @@ block heroes-comp-add
431434
In our current implementation we are just logging the error to the console, but a real life application should do better.
432435

433436
### Import operators
434-
These operators we are not included by default, so we have to load each one using `import` statements.
437+
The `RxJs` operators are not included by default, so we have to include them using `import` statements.
435438

436439
We have combined all operator imports in a single file.
437440

@@ -448,13 +451,6 @@ block heroes-comp-add
448451
figure.image-display
449452
img(src='/resources/images/devguide/toh/toh-hero-search.png' alt="Hero Search Component")
450453

451-
block review
452-
:marked
453-
### Let's see it
454-
Here are the fruits of labor in action:
455-
figure.image-display
456-
img(src='/resources/images/devguide/toh/toh-http.anim.gif' alt="Heroes List Editting w/ HTTP")
457-
458454
.l-main-section
459455
:marked
460456
## Application structure and code

0 commit comments

Comments
 (0)