How to get Ionic page informed about some event from provider?


#1

I am really struggling with this one, right now I am using chart.js in my page, and I download data from my server every 1 minute in a provider class, so I also have to refresh the chart every 1 minute, and I dont know how to get my page informed that the chart should be refreshed at some point in time. Is there any way to do that?


#2

Expose an Observable (some flavor of Subject would probably be most straightforward) in the provider that makes the requests that emits new values every time new data comes in. Subscribe to it in your page and update a page property in your subscription.


#3

I was trying that but because there is an async call in my provider(API request) the chart refresh function is actually getting done before the new data arrives


#4

This doesn’t make sense to me. The new data arriving is what triggers a new emission in the Subject, which triggers a new chart refresh. The chart can’t update too early because the property it’s relying on doesn’t change until the subscription fires.


#5

ok, so how can I implement that?
I think I understand what you are saying but still I have no clue about some keyword that I am supposed to use
this is my data function that got called every minute:

      let startTime = new Date(Math.ceil(new Date().getTime() / 60000) * 60000);
      this.Source = Rx.Observable.timer(startTime, 60000).timeInterval().pluck('interval');

      this.Subscription = this.Source
        .subscribe(data => {

              this.http.post(this.ConnectionString + '/api/MacMyWork/Ping', JSON.stringify({ headers: headers })
                .map(res => res.json())
                .subscribe(data => {
                 //code inside
                  resolve(data);
                }, (err) => {
                  reject(err);
                });
        });

#6

or maybe I will just move observable into the page class and it will trigger the event from provider which will resolve into chart refresh, I think it will be simpler to make

edit: no, this isn’t good idea, nvm


#7

are you still there? :frowning:


#8

Did you manage to solve this problem?


#9

yeah but its not like a problem, just design pattern