Created
May 20, 2018 20:38
-
-
Save nerdic-coder/39021b8a1876dd296f45802616fcea96 to your computer and use it in GitHub Desktop.
searchHeroes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* GET heroes whose name contains search term */ | |
searchHeroes(term: string): Observable<Hero[]> { | |
if (!term.trim()) { | |
// if not search term, return empty hero array. | |
return of([]); | |
} | |
return Observable.create((observer) => { | |
const xhr = new XMLHttpRequest(); | |
xhr.open('GET', CONFIG.SERVER_URL + `heroes/?q=${term}`); | |
xhr.onload = () => { | |
if (xhr.status === 200) { | |
this.messageService.add(`HeroService: searched heroes`); | |
observer.next(JSON.parse(xhr.responseText)); | |
} | |
else { | |
observer.error(xhr.response); | |
} | |
}; | |
xhr.send(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment