Computed property is not working for ember cli when route pass value to controller


#1

I created a setup controller to assign value of the countries for select2 input, im having trouble accessing the data in the controller

// location/edit.js   
 import Ember from 'ember';

export default Ember.Route.extend({
    model:function(params){
        //console.log(this.modelFor('location'));
        var location = this.store.find('location', params.location_id);
        return location;
    },
    setupController: function(controller, model) {
        controller.set('model', model);
        controller.set('countries', this.store.find('country'));
    }
});


//controller/location/edit.js
import Ember from 'ember';

export default Ember.ObjectController.extend({

 countryList: function(){

        console.log(this.get('countries'));
        var countries = this.get('countries');
        var myCountries =[];

        countries.forEach(function(country){

            myCountries.push({
                id: country.get('id'),
                text: country.get('name'),
                value:country
            });
            return myCountries;

        });
    }.property('countries.@each.name')
});

#2

You are making a find in the setupController method. This method doesnt wait for promises you return. For this case you have the afterModel hook or the beforeModel hook. So when you set the model of the CountriesController, you are setting a promise on it, which aint working of course.

you can load the countries by return the find promise in one of the mentioned hooks. And in the setupController method, you can write:

controller.set('countries', this.store.all('country'));

the “all” method instantly gives you all the countries in the store without making a fetch from the backend or returning a promise.


#3

The route will wait for the aftermodel hook to resolve prior to pass to setupController? In any case, you don’t have access to the controller in any of the *model hooks, so you probably want to perform a store.find in any of them and, if the router would wait for the promise to return, set the countries in the setupController hook as @gerrit said.