Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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