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

docs(toh-6): search query fixes #2008

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions public/docs/_examples/toh-6/ts/app/dashboard.component.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<!-- #docregion -->
<h3>Top Heroes</h3>
<div class="grid grid-pad">
<!-- #docregion click -->
<div *ngFor="let hero of heroes" (click)="gotoDetail(hero)" class="col-1-4">
<!-- #enddocregion click -->
<div class="module hero">
<h4>{{hero.name}}</h4>
</div>
</div>
</div>
<hero-search></hero-search>

18 changes: 8 additions & 10 deletions public/docs/_examples/toh-6/ts/app/hero-search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,31 @@ export class HeroSearchComponent implements OnInit {
// #docregion search
heroes: Observable<Hero[]>;
// #enddocregion search
// #docregion searchSubject
searchSubject = new Subject<string>();
// #enddocregion searchSubject
// #docregion searchTerms
searchTerms = new Subject<string>();
// #enddocregion searchTerms

constructor(
private heroSearchService: HeroSearchService,
private router: Router) {}
// #docregion searchSubject
// #docregion searchTerms

// Push a search term into the observable stream.
search(term: string) { this.searchSubject.next(term); }
// #enddocregion searchSubject
search(term: string) { this.searchTerms.next(term); }
// #enddocregion searchTerms
// #docregion search

ngOnInit() {
this.heroes = this.searchSubject
.asObservable() // cast as Observable
this.heroes = this.searchTerms
.debounceTime(300) // wait for 300ms pause in events
.distinctUntilChanged() // ignore if next search term is same as previous
.switchMap(term => term // switch to new observable each time
// return the http search observable
? this.heroSearchService.search(term)
// or the observable of empty heroes if no search term
: Observable.of<Hero[]>([]))

.catch(error => {
// Todo: real error handling
// TODO: real error handling
console.log(error);
return Observable.of<Hero[]>([]);
});
Expand Down
4 changes: 1 addition & 3 deletions public/docs/_examples/toh-6/ts/app/hero-search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ export class HeroSearchService {

constructor(private http: Http) {}

// #docregion observable-search
search(term: string) {
return this.http
.get(`app/heroes/?name=${term}+`)
.get(`app/heroes/?name=${term}`)
.map((r: Response) => r.json().data as Hero[]);
}
// #enddocregion observable-search
}
14 changes: 6 additions & 8 deletions public/docs/ts/latest/tutorial/toh-pt6.jade
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,11 @@ block observables-section
Time to create the `HeroSearchComponent` class and metadata.
+makeExample('toh-6/ts/app/hero-search.component.ts', null,'hero-search.component.ts')
:marked
Focus on the `searchSubject`.
+makeExample('toh-6/ts/app/hero-search.component.ts', 'searchSubject')(format=".")
Focus on the `searchTerms`.
+makeExample('toh-6/ts/app/hero-search.component.ts', 'searchTerms')(format=".")
:marked
A `Subject` is a producer of an _observable_ event stream.
This `searchSubject` produces an `Observable` of strings, the filter criteria for the name search.
This `searchTerms` produces an `Observable` of strings, the filter criteria for the name search.

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

Expand All @@ -439,8 +439,6 @@ block observables-section
Fortunately we can chain `Observable` operators to the string `Observable` that reduce the request flow.
We'll make fewer calls to the `HeroSearchService` and still get timely results. Here's how:

* The `asObservable` operator casts the `Subject` as an `Observable` of filter strings.

* `debounceTime(300)` waits until the flow of new string events pauses for 300 milliseconds
before passing along the latest string. We'll never make requests more frequently than 300ms.

Expand Down Expand Up @@ -495,11 +493,11 @@ block observables-section
+makeExample('toh-6/ts/app/app.component.ts', 'rxjs-extensions', 'app/app.component.ts')(format=".")

:marked
### Adding the search component to the dashboard
### Add the search component to the dashboard

We add the `HeroSearchComponent` to the bottom of the `DashboardComponent` template.
We add the hero search HTML element to the bottom of the `DashboardComponent` template.

+makeExample('toh-6/ts/app/dashboard.component.html', null, 'dashboard.component.html')
+makeExample('app/dashboard.component.html')

:marked
And finally, we import the `HeroSearchComponent` and add it to the `directives` array.
Expand Down