ember.js: replaceWith / transitionTo doesn't respect query params on beforeModel
As described in https://github.com/emberjs/ember.js/issues/12824#issuecomment-260449258, I’m attempting to use replaceWith to set a default query param value that I want to be visible in the URL.
Expected behavior:
On initial load, the page should output “Query timeframe: 24hrs” with the desired value of the timeframe query param (24hrs).
Actual result:
The redirect does not take place, and the query param is not set to any value. The page instead outputs "Query timeframe: "
If you set the timeframe query param manually, the output reflects the actual value of that param. Try changing the path box on the Twiddle to /?timeframe=24hrs.
cc @rwjblue
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 22 (12 by maintainers)
Commits related to this issue
- Add Zoey says note about query params when redirecting Closes https://github.com/emberjs/ember.js/issues/14606 — committed to mehulkar/guides-source by deleted user 4 years ago
- Add Zoey says note about query params when redirecting Closes https://github.com/emberjs/ember.js/issues/14606 — committed to lenoraporter/guides-source by deleted user 4 years ago
@bgentry This seems like a bug seems that
replaceWithandtransitionto do not work with a{queryParams:{name:'value'}argument; but work fine with using an argument for a route name.Docs here, http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo, indicate that the following should work, but the above that is not working in the above twiddle.
Adding the following to a Controller for my
/auth/loginroute worked. Hopefully this helps others.For what it’s worth, the documentation about Redirecting does not state that query parameters must be declared in the destination Controller, but it is mentioned on the Query Parameter docs. That’s probably how I missed it. Making this more explicit in the docs may help other Ember newbies like myself.
Hmm yeah, thinking about it, the transition should work, as there are totally legit usecases for doing this unrelated to param defaults.
I wouldn’t advise doing pretty much anything in init methods most anywhere in ember, with the exception of basic object setup - you have no guarantees about when your init method would be called, it may not be the point you expect due to dependency injection laziness.
I don’t use controllers at all (I don’t have a use for them and I know they are being deprecated).
The routing guides state that redirection / transitioning in
beforeModelis indeed supported. It’s something about the query-only case I think.This doesn’t seem to be related to the recent transition work I did.
You’re not really transitioning, because your query params is not
refreshModel. If you really need the default param serialized to the url, I suggest usingrefreshModelor usingsetinsetupController(requires arun.next) instead, both of which do work currently.However I think the real issue is fighting against the design of query params, which is explicitly that the default value doesn’t show in the url. Your use case is explicitly not support by ember currently so whilst there are some workarounds, I’m inclined to say this isn’t a bug, it’s just not how query params work