问题
I have a route main
which has a query param search
.
I have subroutes main.sub1
, main.sub2
Going to /main/sub1?search=hello
or /main/sub2?search=hello
sets the query param of the main
route to hello
. This part works fine.
Now I would like to be able to refresh the model of main.sub1
when the query param search
in route main
changes, so I here is the code of the route main.sub1
:
export default Ember.Route.extend({
queryParams: {
search: {
refreshModel: true
}
},
model(params) {
// here I can use params.search to fetch the model for this route
}
}
I thought that since there is no search
query param in the controller of main.sub1
Ember would be smart enough to guess it has to use the one from main
. But now, when I go to /main/sub1
I have this error message:
Assertion Failed: You're not allowed to have more than one controller property map to the same query param key, but both
main:search
andmain.sub1:search
map tosearch
. You can fix this by mapping one of the controller properties to a different query param key via theas
config option, e.g.search: { as: 'other-search' }
My guess is that Ember automatically creates a query param search
in main.sub1
which conflicts with the one from main
, and does not even try to use the one from main
.
How could I overcome this problem?
Simply put: how can I use an attribute of a parent route as a query param of a subroute?
Thanks!
回答1:
I had the same issue.
It is completely enough to use the queryParams of the "main" route. You don't need to add any queryParams to the sub-routes. The refreshModel setting belongs to the main-route and all child routes will refresh as well.
Also you can use for the sub-routes model:
this.paramsFor(‘main’)
See: https://discuss.emberjs.com/t/refresh-model-when-the-queryparam-of-a-parent-route-changes/14903/2
来源:https://stackoverflow.com/questions/36202562/use-query-params-of-parent-route-to-refresh-the-model-in-subroute