Skip to content

Instantly share code, notes, and snippets.

@nerdic-coder
Last active May 13, 2018 15:41
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/552e35c6d94dcad1446ec169e022ec82 to your computer and use it in GitHub Desktop.
Save nerdic-coder/552e35c6d94dcad1446ec169e022ec82 to your computer and use it in GitHub Desktop.
deleteHeroService.ts
/** DELETE: delete the hero from the server */
deleteHero (hero: Hero | number): Observable<Hero> {
return Observable.create((observer) => {
const id = typeof hero === 'number' ? hero : hero.id;
const xhr = new XMLHttpRequest();
xhr.open('DELETE', CONFIG.SERVER_URL + `heroes/${id}`, true);
xhr.onload = () => {
if (xhr.status === 200) {
this.messageService.add(`HeroService: deleted hero with id:${id}`);
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