Skip to content

Instantly share code, notes, and snippets.

@nerdic-coder
Created May 20, 2018 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerdic-coder/39021b8a1876dd296f45802616fcea96 to your computer and use it in GitHub Desktop.
Save nerdic-coder/39021b8a1876dd296f45802616fcea96 to your computer and use it in GitHub Desktop.
searchHeroes.ts
/* 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