2

I am developing SPA using EmberJS latest 1.7.0, according to Ember docs, the route contains a property that holds a reference to the controller, but it always returns undefined in all my routes.

ApplicationRoute = Ember.Route.extend({
  queryParams: {
      tsk: {
          // Opt into full transition
          refreshModel: true
      }
  },
  model: function(params) {
      var task = {"name" : "task"};

      this.controller.set('currentTask',task);
      return task;
  }
});

and also in another route

SubfileRoute = Ember.Route.extend({
  model : function(params) {

      console.log(this.controllerName);
      console.log(this.controller);

  }
});

both console.log(this.controllerName); and console.log(this.controller); are undefined,

how to properly work with ember controllers? only way that seems to be working is through this.controllerFor()

Update:

I also tried to use this method:

needs:'application',
currentTask : Ember.computed.alias('controllers.application.currentTask'),

but also the currentTask is always undefined, so as the the controller it self.

engma
  • 1,849
  • 2
  • 26
  • 55
  • possible duplicate of [How to access controller from route?](http://stackoverflow.com/questions/15911704/how-to-access-controller-from-route) –  Sep 29 '14 at 12:24
  • Accessing the controller from the route is an anti-pattern, except when it is passed into you as in `setupController`. –  Sep 29 '14 at 12:25
  • this question is not about how to access the controller, this is as to why a certain property is undefined – engma Sep 29 '14 at 12:51
  • `controller` is undefined on Route because it is not defined on the Route class. See the API docsc at http://emberjs.com/api/classes/Ember.Route.html. You have to jump through hoops such as with `controllerFor`. Also, `needs` is not available on routes, it's just for controllers. Bottom line is, if you want to do something with your controller, then, well, do it in your controller. –  Sep 29 '14 at 13:05
  • ok, the reason I am doing this is because I need something for global use, I would just rely on the model return and that would be it, but I set the property to use in everywhere in the app, and I learned global variables is not good. – engma Sep 29 '14 at 13:17

1 Answers1

5

If you want to set properties on the controller, you should use the setupController hook:

model: function(params) {
  return {"name" : "task"};
},

setupController: function(controller, task) {
  controller.set('currentTask',task);
}
Peter Brown
  • 50,956
  • 18
  • 113
  • 146
  • but then what is the property of this.controller, I know those methods, but I found this method which always returns undefined, it is said in the docs, that it refers to the current controller, and why whould I use controllerFor in the current controller ? – engma Sep 29 '14 at 12:58
  • @Developer106 this.controller is not a method that always returns undefined. You are trying to access a property on `this`(the route object), which has no controller property. Use the setupController hook (its where you write the exact type of code you are trying to write). – mistahenry Sep 29 '14 at 18:43
  • thanks alot that worked, but then what is [this](http://emberjs.com/api/classes/Ember.Route.html#property_controller) used for? – engma Sep 29 '14 at 21:07
  • @engma I have the same question. Time and time again I wish javascript never had 'this' for developers to mess with. It is never obvious what 'this' is in libraries, it is always undocumented, and turns into game of "log and find out" – aaaaaa Apr 30 '17 at 19:52
  • `var context = this.controllerFor('controller-name');` 'this' would refer to the 'route' you are in + access a method that gets a controller of your choosing. – sheriffderek Jul 13 '17 at 03:28