Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class StuffService {
- constructor(private http: Http, private alert: AlertService) {}
- // 1. RxJs
- // 2. Handling error in component (caller)
- // 3. Results for caller only
- getAll(): Observable<Stuff[]> {
- return this.http.get('/stuff/all')
- .map(res => res.json());
- }
- // Subscribe for this to get the results
- stuffs = new BehaviorSubject<Stuff[]>([]);
- // 1. RxJs
- // 2. Handling error in service
- // 3. Results for everyone
- getAll2(): void {
- this.http.get('/stuff/all')
- .map(res => res.json())
- .subscribe(
- res => this.stuffs.next(res),
- err => this.alert.errorPopup('Refresh failed'),
- );
- }
- // 1. Async-await (Promise)
- // 2. Handling error in component
- // 3. Update results for caller only; and it also updates the `stuffs` observable
- async update(item: Stuff) {
- let result = await this.http.post('/stuff/update', item)
- .map(res => res.json())
- .toPromise();
- // Auto-refreshing the list, each subscriber gets the update
- this.getAll2();
- return result;
- }
- }
- .post()
- .map()
- .catch()
Add Comment
Please, Sign In to add comment