vendredi 25 août 2017

Can't get value of array inside computed property

I'm having trouble with a computed property in Ember.

The problematic item is timeZones, which is set like this:

import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return Ember.RSVP.hash({
      account: this.store.findRecord('account', params.id),
      timeZones: this.store.findAll('time-zone'), <------------ timeZones
      users: this.store.query('user', { by_account_id: params.id })
    });
  },

  setupController(controller, model) {
    this._super(controller, model.account);
    controller.set('users', model.users);
    controller.set('timeZones', model.timeZones);
  }
});

Then I have something called selectedTimeZone which looks like this:

  selectedTimeZone: Ember.computed('location.timezone', 'timeZones', function() {
    console.log(this.get('timeZones'));

    const timeZoneName = this.get('location.timezone');
    var result;

    this.get('timeZones').forEach(function(timeZone) {
      if (timeZone.name === timeZoneName) {
        console.log('yes'); // <------------------- never gets here
        result = timeZone;
      }
    });

    return result;
  }),

The problem is that this.get('timeZones') isn't really accessible inside the component. timeZones makes it to the template just fine. I'm populating a dropdown with timeZones right now. But when I console.log it, it just comes through as Class.

How can I get my hands on timeZones inside this computed property?




Aucun commentaire:

Enregistrer un commentaire