mercredi 17 octobre 2018

Updating ember-moment element when reloading a route in Ember?

I have an Ember app that uses ember-moment.

The application defines a model for site objects:

import DS from 'ember-data';

export default DS.Model.extend({
  lastPoll: DS.attr('date'),
  status: DS.attr('string')
});

The index route loads site #1 when activated:

import Route from '@ember/routing/route';

export default Route.extend({
  model(params) {
    return this.store.findRecord('site', 1);
  }
});

The template for this route is fairly simple:

: 

All of this works as intended. However, I would like to reload the route every thirty seconds.

Thus, I have expanded the route as follows:

import Route from '@ember/routing/route';

export default Route.extend({
  model(params) {
    return this.store.findRecord('site', 1);
  },

  afterModel() {
    this.set('_timer',
      Ember.run.later(this, () => {
        this.refresh();
      }, 30 * 1000)
    );
  },

  deactivate() {
    Ember.run.cancel(this.get('_timer'));
  }
});

The status updates when the data returned by the API changes. However, the relative time displayed in the template does not change. I am assuming Ember sees that lastPoll has not changed and does not update the element containing the relative time, even though the current time has changed (and therefore the relative time as well).

What is the correct approach for ensuring that the relative time is updated when the route is reloaded?




Aucun commentaire:

Enregistrer un commentaire