Route vs Controller


#1

If I pass the data from the Route’s model as well as the controller, which of them will be used? Say for example, I pass value=A from route and value=B from the controller and when the value is accessed which one will be printed?

Also, why does it happen so ?


#2

As far as I know, what is actually happening when you enter a route is:

  1. The route model() hook is called
  2. Once that resolves, the route setupController(controller, model) hook is called, which by default basically does set(controller, 'model', model)

So I guess the route will overwrite whatever you set as model on the controller once the user navigates there. Also see https://emberjs.com/api/ember/3.7/classes/Route/methods/setupController?anchor=setupController for details on that


#3

Thanks for your reply ! So as you said, the setupController hook will be called after the model hook. In the setup controller method, I’m passing the model for that particular controller. How to access the model that is passed from the route in the controller? Which hook to use ?


#4

In the controller you can just access it via this.model. There is no hook on the controller for that, any setup you need to do based on this can/should be done on the route. E.g. if you want the setupMyModel() method on the controller to be run, you could have a setupController method on the route like this:

setupController(controller, model) {
  this._super(...arguments);
  controller.setupMyModel();
}

#5

@francesconovy Thank you. Will try that ! :slight_smile:


#6

If you interested, there is a whole free tutorial which teach you the main concept behind Ember.js. Up to date: https://yoember.com