Reputation: 83
I have set the queryParam setting in the route to refresh the model if the query param changes. However when the query param changes setupController
is not called. Any idea why this is happening?
export default Route.extend({
queryParams: {
id: {
refreshModel: true
}
},
setupController(controller) {
controller.start(); // fetches data synchronously from api
}
});
// Solution thanks to alptugd's reply
export default Route.extend({
queryParams: {
id: {
refreshModel: true
}
},
model() {
// Returning a new array/object/value everytime the hook is called, causes the
// setupController hook to be called.
return [];
}
setupController(controller) {
controller.start(); // fetches data synchronously from api
}
});
Upvotes: 4
Views: 1313
Reputation: 3368
refreshModel
forces the route to refresh
and as expected you should expect beforeModel
, model
, afterModel
, and setupController
hooks to run. However; there is a minor difference for setupController
hook:
Take a look at the comment for refresh
method of Ember's route.js
in the source code or relevant API documentation. Both say:
"Refresh the model on this route and any child routes, firing the beforeModel
, model
, and afterModel
hooks in a similar fashion to how routes are entered when transitioning in from other route. The current route params (e.g. article_id
) will be passed in to the respective model hooks, and if a different model is returned, setupController
and associated route hooks will re-fire as well."
This means; in order for the setupController
to trigger in case a refresh
occurs; a different model should be returned from the model
hook. In your case; you do not have any model hook
; hence setupController
will not be called upon id
param value change.
By the way; it is a good practice to call super method in case you override setupController
hook since its sole purpose is to save the model
property to the controller
. Take a look at the API for a detailed explanation regarding this case.
Upvotes: 4