Ember shorthand for self.controllerFor on same route/controller


#1

Is there a shorthand for the following code (i.e. calling controller attribute from the same named route)

self.controllerFor(self.routeName).get(‘myAttribute’)

I have already tried self.controller.get(‘myAttribute’)


#2

Within your route, you can do this.get('controller').


#3

Thanks. But I get that as “undefined”


#4

May I know where did you cal this.get(‘controller’) ?

Because, before calling this.get(‘controller’), the controller must be associated to the route. This happens in

setupController(controller, model) { //Your code }


#5
Ember.Route.reopen({
  getControllerAttr: function(controllerName, propertyName) {
     return this.controllerFor(controllerName).get(propertyName); 
  },
  
  getControllerAttr2: function(propertyName) {
     return this.controllerFor(this.get('routeName')).get(propertyName);  
  }
});

https://ember-twiddle.com/1dd0795dafebc846dc38?openFiles=my-route.route.js%2Capplication.template.hbs


#6

I don’t write the setupController since it is the same route/controller & I do not have any additional custom code


#7

@Sarath I did this in a route action.


#8

But is it necessary to write setupController to get this shorthand to work ?