jeudi 30 juillet 2020

Is it possible to make a route's model a computed / tracked property?

I'm trying to have a route's model hook return some array that is constantly updated via a polling mechanism using Ember later. The route file looks like this:

export default class IndexRoute extends Route {
  recent: [],

  init() {
    ...
    this.getRecent();
  }

  getRecent() {
    // poll data / fetch latest
    this.recent.push(newStuff);
    
    later(this, this.getRecent, 2000);
  }

  model() {
    return this.recent;
  }
}
 

Then in my controller, I wanted to create a @computed / @tracked property based on the route's model:

export default class IndexController extends Controller {
  // @tracked model; // this also didn't work
  @computed('model.@each') // this doesn't work
  get computedModel() {
    console.log('computedModel'); // prints only once, when the model hook is first run
    return this.model;
  }
}

I thought what this SO post suggested would have worked but it didn't :(

I saw this post but this was for Ember 1.13 so not exactly a modern solution.

Similarly this post had outdated content too.

Is what I'm trying to do possible? Alternatively I was thinking of moving the data into the Controller and making a computed property of a Controller variable instead. Taking all suggestions!




Aucun commentaire:

Enregistrer un commentaire