From afd951ec21f1de93fa97087b647fefcb32bcb557 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sun, 25 Jun 2017 01:29:11 -0400 Subject: [PATCH] got switchmap working --- src/app/search/search.component.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/app/search/search.component.ts b/src/app/search/search.component.ts index bf418b8..deb2fbb 100644 --- a/src/app/search/search.component.ts +++ b/src/app/search/search.component.ts @@ -1,8 +1,16 @@ import { Component, OnInit } from '@angular/core'; import { Http } from '@angular/http'; + +import { Observable } from 'rxjs/Observable'; import { Subject } from 'rxjs/Subject'; +// Observable class extensions +import 'rxjs/add/observable/of'; +import 'rxjs/add/operator/switchMap'; +import 'rxjs/add/operator/map'; + // Observable operators +import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/debounceTime'; import 'rxjs/add/operator/distinctUntilChanged'; @@ -35,12 +43,20 @@ export class SearchComponent implements OnInit { // .then(response => console.log(this.results = response.json().results) ); } + createAPIObservable(name){ + return this.http.get('http://swapi.co/api/people/?search=' + name) + .map((response)=>{ + return response.json().results; + }); + } + ngOnInit() { this.searchTerms .debounceTime(300) .distinctUntilChanged() - .subscribe((term)=>{ - console.log(term); + .switchMap(term => this.createAPIObservable(term)) + .subscribe((results)=>{ + this.results = results; }); }