mercredi 21 octobre 2015

Ember- 1.13 service not initialized when url has a url parameter

I have a clock service -clock.js

import Ember from 'ember';
var ONE_SECOND = 2000;

export default Ember.Object.extend({
  time : null,
  second : null,
  tick: function() {
    var clock = this;

    Ember.run.later(function () {
      var now = new moment();
      clock.setProperties({
        time : now,
        second : now.seconds()
      });
    }, ONE_SECOND);

  }.observes('second').on('init')
})

which I initialize as

export function initialize(container, application) {
  container.optionsForType('service:clock', { singleton: true });
  application.inject('route', 'clockService', 'service:clock');
  application.inject('controller', 'clockService', 'service:clock');
  application.inject('component', 'clockService', 'service:clock');
}

export default {
  name: 'clock-service',
  initialize: initialize
};

I use the clock ticker to compute elapsed time ( I am using moment.js).

timeElapsed : function(){
    var clock = this.get('clockService').get('time');
    var createdTime = new moment(this.get('request.createdAt'));
    var duration = moment.duration(clock.diff(createdTime));
    if(duration._data.hours > 0) return duration._data.hours+" hrs";
    if(duration._data.minutes > 0) return duration._data.minutes+" mins";
    if(duration._data.seconds > 0) return duration._data.seconds+" secs";
    return duration._data.seconds;
  }.property('request.createdAt','clockService.second')

Now the issue is when I open my app at a route which is defined in the router. (say outlet/{{:id}}) it opens up and the clock is properly initialized and I don't get any error.

However if there is a url param (eg. localhost:4200/1?selectedFilter=2) and I reload the page, the service is injected but the time property is not available in my component.

I'll reiterate, the error comes only when I reload page with a url param.




Aucun commentaire:

Enregistrer un commentaire