Access to queryParams from Controller
Disclaimer - I am definitely an Ember newbie...
I am wanting to use the experimental query params feature documented here. So, I have the latest Canary build (1.3.0-beta.1+canary.8f5b6e20). The feature sort of seems to be working, but I am trying to get access to the query params in my controller such that I can populate the field containing the selected value for an Ember.Select.
The documentation page claims I should be able to get access to the query params object in the various route methods as follows:
App.PostsRoute = Ember.Route.extend({
beforeModel: function( queryParams, transition ) {},
model: function( params, queryParams, transition ) {},
afterModel: function( resolvedModel, queryParams, transition ) {},
setupController: function( controller, context, queryParams ) {},
renderTemplate: function( controller, context, queryParams ) {}
});
According to the docs, this should be the 3rd parameter to the setupController() method. However, for me the 3rd parameter is undefined.
The documentation also seems wrong for other methods as well. For instance, it says the model() method should have the query params object as the second parameter. Instead, I was only passed two parameters and I had to get to the query params within the 'transition' object which was passed as the second param (not the third).
So, I'm not sure if this is a documentation issue or a code issue (or both), but I need a way to access the query params inside of my controller. I thought I could do this through setupController(), but that doesn't seem to work. Fixes/suggestions would be appreciated.
@NateRedding how are you doing it through setupController? Also are you enabling the feature flag for query params? I've been using this feature for a while, and the documentation is correct. It would really help if you could setup a jsbin or something like that.
I did stumble upon the fact that the query params are in the model.query object... I wish it was named 'queryParams' for consistency, but oh well. This still seems to be misdocumented, however.
My setupController looks like this now:
setupController: function(controller, model, queryParams) {
var status = model.query ? model.query.status : null;
if (status) {
controller.statusOptions.forEach(function(statusOption) {
if (status == statusOption.value) {
controller.set('currentStatusOption', statusOption);
}
});
} else {
controller.set('currentStatusOption', controller.allStatusOption);
}
}
Also - just wanted to check. Are query params slated for an official release sometime in the near future? I may not want to depend on it if it's going to stay in the experimental state for the unforeseeable future.
@NateRedding there must be something wrong in your setup, queryParams get as the third argument in your setupController, if you could reproduce the issue in a jsbin it would be more helpful, so is easier to nail down where thing are going wrong.
And yes, I think is they will make it into the next release, @rjackson?
I added the third parameter named queryParams - it is always "undefined" however, even though model.query.status is defined when there are query parameters. It seems like it's simply a matter of incorrect documentation. I'll try to set up a jsbin later if you think it is still needed.
Sure, I still believe is a bug in your end, so a jsbin showing how you are setting up things would help a lot.
On Mon, Nov 25, 2013 at 6:33 PM, NateRedding [email protected]:
I added the third parameter named queryParams - it is always "undefined" however, even though model.query.status is defined when there are query parameters. It seems like it's simply a matter of incorrect documentation. I'll try to set up a jsbin later if you think it is still needed.
— Reply to this email directly or view it on GitHubhttps://github.com/emberjs/ember.js/issues/3801#issuecomment-29239230 .
/cc @alexspeller
NateRedding query params definitely work and you must be doing something wrong. Please double check the documentation and if you still have trouble post your code so I can look at what's up.
@NateRedding query params are very likely to be in an official release soon, for some definition of "soon" ;)
setupController: function(controller, model, queryParams) {
var status = model.query ? model.query.status : null;
if (status) {
controller.statusOptions.forEach(function(statusOption) {
if (status == statusOption.value) {
controller.set('currentStatusOption', statusOption);
}
});
} else {
controller.set('currentStatusOption', controller.allStatusOption);
}
}
Definitely don't do this, this is broken and will leave you with bugs in your code. The hook will not be called properly when params change. I'm betting you are not declaring your query params properly for the route. Please re-read the docs carefully to ensure you have configured your routes correctly.
@NateRedding You possible doing:
- Specify the queryParams at the parent "resource"
- Do not specify queryParams at the children "resources", hoping that query params will be "inherited" from parent
At that case, yes, all children will not get proper model hooks at their routes, as this message tells you
http://emberjs.com/guides/routing/query-params/ Note: If you don't specify query params for a given route, then the parameters passed in to the route hooks remains unchanged.
Closing since it sounds pretty definite that this is not a bug.
@NateRedding Did you read the feature flags guide, especially the part about enabling? I know that tripped us up pretty good.