Guest User

Untitled

a guest
Jan 4th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. export class StuffService {
  2. constructor(private http: Http, private alert: AlertService) {}
  3.  
  4. // 1. RxJs
  5. // 2. Handling error in component (caller)
  6. // 3. Results for caller only
  7. getAll(): Observable<Stuff[]> {
  8. return this.http.get('/stuff/all')
  9. .map(res => res.json());
  10. }
  11.  
  12. // Subscribe for this to get the results
  13. stuffs = new BehaviorSubject<Stuff[]>([]);
  14.  
  15. // 1. RxJs
  16. // 2. Handling error in service
  17. // 3. Results for everyone
  18. getAll2(): void {
  19. this.http.get('/stuff/all')
  20. .map(res => res.json())
  21. .subscribe(
  22. res => this.stuffs.next(res),
  23. err => this.alert.errorPopup('Refresh failed'),
  24. );
  25. }
  26.  
  27. // 1. Async-await (Promise)
  28. // 2. Handling error in component
  29. // 3. Update results for caller only; and it also updates the `stuffs` observable
  30. async update(item: Stuff) {
  31. let result = await this.http.post('/stuff/update', item)
  32. .map(res => res.json())
  33. .toPromise();
  34. // Auto-refreshing the list, each subscriber gets the update
  35. this.getAll2();
  36. return result;
  37. }
  38. }
  39.  
  40. .post()
  41. .map()
  42. .catch()
Add Comment
Please, Sign In to add comment