lundi 3 août 2020

How to pass a @tracked object from an Ember route model hook

My question is two-fold:

  1. Where is the best place to put some kind of polling logic - in the route file right?

  2. How do I pass this constantly updating value from the Route to some child component? Labeling some variable as "@tracked" and then passing the tracked variable via the model hook?

Let's say I have something like this:

routes/index.js

export default class IndexRoute extends Route {
  @tracked
  recent: {
    A: 0,
    ...
  },

  constructor() {
    super(...arguments);
    this.getRecent();
  }

  getRecent() {
    // poll data / fetch latest
    const {A, ...partialObject} = this.recent;
    
    this.recent = { ...partialObject, A: <some new value fetched>};;
    
    later(this, this.getRecent, 2000);
  }

  model() {
    return this.recent;
  }
}

application.hbs

<p>constantly updating "this.recent":  </p>

I thought if I use the model hook like this, it would be tracked and therefore auto-update but that was not the case. I have this sample Ember Twiddle that emulates what I'm trying to do. I tried to force a re-compute by reassigning the entire variable but it didn't work.

This question is a deeper dive from my initial question here.




Aucun commentaire:

Enregistrer un commentaire