dimanche 21 mai 2017

Property in Ember Service undefined

I have a service for a business calendar where a property is not available to a computed property but is available in a method.

In the code below the property calendars is not defined when the computed businessYear property is accessed. On the other hand, calendars is defined when the year() method is called.

The currentYear property is used elsewhere so init() is called and the store promise is resolved before businessYear is accessed.

I've debugged this and developer tools shows that this.calenders is defined in watch variables but is still undefined in the computed property. It's almost like the computed property is being executed in another context. I'm probably missing something really basic but just don't see it.

app/services/business-calendar.js

import Ember from 'ember';
import moment from 'moment';

const { computed, inject: { service }, get, set } = Ember;

const findCalendar = (date, calendars) => {
    let p1, p13

   return calendars.find(function (item, index, enumerable) {
       p1 = moment(get(item, 'p1'));
       p13 = moment(get(item, 'p13'));
       return moment.range(p1, p13).contains(date);
   })
}

export default Ember.Service.extend({
   store: service(),
   calendars: [],

currentDate: computed(() => {
    return moment();
}),

currentYear: computed('currentDate', () => {
    return moment(get(this, 'currentDate')).year();
}),

businessYear: computed(() => {
    let calendar = findCalendar(moment(), get(this, 'calendars'));
    return get(this.calendar, 'year');
}),

init() {
    this._super(...arguments);
    get(this, 'store').findAll('calendar').then((recs) => {
        set(this, 'calendars', recs);
    })
},

year(date) {
    let d = moment(date);
      var calendar = findCalendar(d, get(this, 'calendars’));
       return get(calendar, 'year');
  }
});




Aucun commentaire:

Enregistrer un commentaire