Setting value on controller from route leads to undefined when accessed from the controller


#1

I was trying to set a value on a controller from the corresponding route, however I was running into way more issues than I expected.

routes/group.js:

export default Ember.Route.extend({
  setupController(controller, model) {
    this._super(controller, model);
    controller.set('testVar', true);
  }
});

Then I do have for instance an action which uses the variable testVar in the controller:

controller/group/index.js:

export default Ember.Controller.extend({
   actions: {
    create: function() {
      console.warn('testVar: ' + this.get('testVar'));
      console.warn('lookup: ' + this.container.lookup('controller:group').get('testVar'));
    }
  }
});

I would expect both statements to log testVar as ‘true’, however the actual application output is:

testVar: undefined
lookup: true

I have tried to use different variable names to make sure nothing is getting overridden by another function (just trying to exclude anything that could be causing such trouble).

However by now I am suspecting that setupController is handling a different instance than that of the actual controller - just a guess into the wild. It seems quite weird to me that if I look up the controller for ‘group’ within the controller ‘group’ that I am ending up with different results.

If I log controller from setupController in the route I get:

controller
Class {__ember1484071323549: null}

If I log this from within the controller I get:

this
Class {__ember1484071323549: "ember574"

Not sure if that is even related. Either way I am doing something fundamentally wrong (however I found a ton of examples doing it that way), something has changed (couldn’t find anything related so far), or maybe something is broken. Please share your thoughts and suspicions as by now it has become quite frustrating that it has gotten that complicated to do something which seems to be that simple.

ember version is : 2.10.2


#2

That’s because you’re setup controller hook is for a different controller. routes/group.js will setup the controllers/group.js controller, whereas routes/group/index.js will setup the controllers/group/index.js controller (notice the similarity with the paths).


#3

Ah, thank you a lot! That makes sense. I am not really sure what has caused my problems with using templates/group.hbs , but maybe it was the default controller back than. Anyways… Ember seems to be doing now what I was hoping it would be doing :slight_smile: