2016-02-02 02:27:52 -08:00
|
|
|
// #docregion
|
2016-05-03 14:06:32 +02:00
|
|
|
import { Component } from '@angular/core';
|
|
|
|
import { JSONP_PROVIDERS } from '@angular/http';
|
|
|
|
import { Observable } from 'rxjs/Observable';
|
2016-02-03 22:28:39 -08:00
|
|
|
// #docregion import-subject
|
2016-05-03 14:06:32 +02:00
|
|
|
import { Subject } from 'rxjs/Subject';
|
2016-02-03 22:28:39 -08:00
|
|
|
// #enddocregion import-subject
|
2016-02-02 11:22:48 -08:00
|
|
|
|
2016-05-03 14:06:32 +02:00
|
|
|
import { WikipediaService } from './wikipedia.service';
|
2016-02-02 02:27:52 -08:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-wiki-smart',
|
|
|
|
template: `
|
|
|
|
<h1>Smarter Wikipedia Demo</h1>
|
|
|
|
<p><i>Fetches when typing stops</i></p>
|
|
|
|
|
|
|
|
<input #term (keyup)="search(term.value)"/>
|
|
|
|
|
|
|
|
<ul>
|
2016-04-29 01:36:35 +01:00
|
|
|
<li *ngFor="let item of items | async">{{item}}</li>
|
2016-02-02 02:27:52 -08:00
|
|
|
</ul>
|
|
|
|
`,
|
2016-05-17 01:45:52 -07:00
|
|
|
providers: [JSONP_PROVIDERS, WikipediaService]
|
2016-02-02 02:27:52 -08:00
|
|
|
})
|
|
|
|
export class WikiSmartComponent {
|
|
|
|
|
2016-05-03 14:06:32 +02:00
|
|
|
constructor (private wikipediaService: WikipediaService) { }
|
2016-02-02 02:27:52 -08:00
|
|
|
|
2016-02-03 22:28:39 -08:00
|
|
|
// #docregion subject
|
2016-05-03 14:06:32 +02:00
|
|
|
private searchTermStream = new Subject<string>();
|
2016-02-02 11:22:48 -08:00
|
|
|
|
2016-05-17 01:45:52 -07:00
|
|
|
search(term: string) { this.searchTermStream.next(term); }
|
2016-02-03 22:28:39 -08:00
|
|
|
// #enddocregion subject
|
2016-02-02 11:22:48 -08:00
|
|
|
|
|
|
|
// #docregion observable-operators
|
2016-05-17 01:45:52 -07:00
|
|
|
items: Observable<string[]> = this.searchTermStream
|
2016-02-02 11:22:48 -08:00
|
|
|
.debounceTime(300)
|
|
|
|
.distinctUntilChanged()
|
2016-05-17 01:45:52 -07:00
|
|
|
.switchMap((term: string) => this.wikipediaService.search(term));
|
2016-02-02 11:22:48 -08:00
|
|
|
// #enddocregion observable-operators
|
2016-02-02 02:27:52 -08:00
|
|
|
}
|