1

I want a component to call a method which calls a http.post which sets a behavior subject so then other components have access to the data some thing like this:


Component:

ngOnInit():void {
    this.service.getAsync();
}

Service:

  private _subject = new BehaviorSubject<any>([]);
  subject$ = this._subject.asObservable();

 getAsync() {    
    this.subject$ =  this.http
      .post(url, payload, options)
      .map();
  }

Or should I be looking to do something like this:

Component:

ngOnInit():void {
    this.service.setAsync();
}

Service:

  private _subject = new BehaviorSubject<any>([]);
  subject$ = this._subject.asObservable();

  setAsync(){
    this.subject$ = this.getAsync();
  }


  getAsync(): Observable<any[]> {    
    return this.http
      .post(url, payload, options)
      .map();
  }

Or something like this

Component:

ngOnInit():void {
    this.service.getAsync();
}

Service:

  private _subject = new BehaviorSubject<any>([]);
  subject$ = this._subject.asObservable();

  getAsync() {    
    this.http
      .post(url, payload, options)
      .map(data => this._subject.next(data));
  }

I'm not sure of what makes the most sense. To me 1 & 3 are the most logical. But I see everyone else using the return of an observable pattern. Then of course there is the 4th option that all of the above are wrong. What's the best way to do this?

1 Answer 1

3

I would do this:

getAsync() {    
  this.http
    .post(url, payload, options)
    .map(t=>t.json())
    .subscribe(t=> {
        this._subject.next(t);            
    });
}
Sign up to request clarification or add additional context in comments.

6 Comments

This makes even more sense to me. This should work in the situation where the payload changes so I could just call the same method again correct?
One other question, are there any disadvantages to just setting the http call equal to the observable? e.g. subject$ = this.http.post()
If you're going to overwrite the subject$ event, then the Subject is not needed. The benefit to having a subject is that you can fire the event yourself, as many times as you want. An http observable is a one time event. I think either is fine, just depends on your intent.
So with the intent to possibly use multiple http observables it makes the most sense to use a subject and just use .next with each new http call since it's a one time observable?
In event based programming, you may want to setup a service, where components can subscribe to events that the service might expose. In that case, a subject would make sense, since you can setup an event, and have multiple components subscribe to that event. In other cases, you just want to make an http call, and notify components when it happens.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.