Pass Params from 'Model' Hook to 'setupController' hook in route handler

I am looking to Pass route params from ‘Model’ Hook to ‘setupController’ hook in a route handler.

I can’t seem to achieve this without first doing the following in my model hook this.set('params',params);

Yeah… the different hooks receive different args and setupController does not get the route params. What you’re doing is fine (setting the params on the route class and getting them from setupController) but the easiest way to pass the params to the controller is just to set the params on the model before you return it from the model hook, then you can access them in the controller/route template with [this.]model.params.<xyz>. Then you don’t even need to use the setupController hook at all.

1 Like

Thanks.

I am having trouble finding up to date concrete documentation for what I would consider pretty basic functionality in a standard web app.

Would you have any advice for someone looking to jump into Ember.js in 2020?

Thanks, g

Yeah it can be difficult especially if you’re coming from Google (which often links to out of date information). Obviously the official guides and API docs are the best resource. The guides are great about walking through the basic concepts, but obviously lack (intentionally) a lot of the detail and nuance the API docs provide. I find myself looking at the API docs the most personally. They can be a bit hard to navigate if you aren’t familiar with the modules, but there’s also an index by Class which should help a lot of typical use cases.

For example looking for the setupController hook just head to the API docs, find “Route” under the class index, and you’ll find setupController under methods.

And now that I mention it, it looks like transition was added to the setupController args in 3.17, so if you’re on that version you could disregard what I told you above and get the params from the transition object :grin:

As far as other resources go…

Lastly, in addition to keeping an eye on and/or posting in this discourse forum, Ember has a great community in Discord where you can ask questions or see news, etc so I would highly recommend checking that out as well if you haven’t.

Hi,

There is another few ways to do it:

  1. this.paramsFor(‘your_route’) in setupController

  2. Like dknutsen mentions, take them from the RouteInfo of the transition object that is passed into setupController() transition.to.params.

But with the “to” you have to bear in mind it’s the tail of the route so if ember.js has (like the ball-ache it can be) taken the liberty of moving into and appending “.index” to your route, it will appear like params is empty which means you gotta look up one level to get them. Something like this:

    let routeinfo = transition.to.find(function(item, index, array) {

    // Bit daft that I have to do this but it's because the .index does not get the params passed

        return typeof(item.params.cat_id) != "undefined";

    });

    

    controller.set('exitroute', routeinfo ? routeinfo : transition.from);