I am confused about queryParams from Ember's documentation. It shows that you can put queryParams to Controller or Route. What are the differences having queryParams in Route vs Controller except syntax?
As far as I understand, having queryParams as object in Route allows me to do following things:
In what case would you choose to have queryParams in Controller?
By default, query param values in Ember are "sticky", in that if you make changes to a query param and then leave and re-enter the route, the new value of that query param will be preserved (rather than reset to its default).
To generate a controller, run ember generate controller my-controller-name This creates a controller file at app/controllers/my-controller-name.js, and a unit test file at tests/unit/controllers/my-controller-name-test.js. The controller name my-controller-name must match the name of the Route that renders it.
If you have a category query param and you want it to trigger a model refresh, you can set it as follows: By default, Ember will use pushState to update the URL in the address bar in response to a controller query param property change.
When a controller's query param property is currently set to its default value, this value won't be serialized into the URL. So in the above example, if page is 1, the URL might look like /articles , but once someone sets the controller's page value to 2, the URL will become /articles?page=2. The query params are defined per route/controller.
You can use the controller to set default values for queryParams, bind queryParam values to values in your template, and update queryParam values. When these values change in the controller, the controller updates the url, and the route takes the url values so that you can then make ember-data requests.
Let's say you're rendering a paginated list of items, with pagination controls on the page. On the initial page load, you load the first page of results from the API. In order to link up that 'next page' action to load the next set of results, you need to use the controller to update the queryParams.
Your route might look like this:
export default Route.extend({
queryParams: {
pageNumber: {
refreshModel: true //when we set this to true, every time the pageNumber param changes, the model hook below will refresh and the data set will update.
}
},
model(params) {
return this.get('store').query('items', params);
}
});
And your controller might look like this:
export default Controller.extend({
queryParams: ['pageNumber'],
pageNumber: 1,
actions: {
nextPage () {
const newPageNumber = this.get('pageNumber') + 1;
this.set('pageNumber', newPageNumber);
}
}
});
When you update the pageNumber
attribute in your controller, this will bind to the route and refresh the model, loading the next page of data.
Essentially the controller is there if you need to modify any queryParams from your template. You might have a list of data that can be filtered, paged, updated etc., and you can control these parameters using the controller.
You can also set default values for params in the controller.
Hope this is helpful! (:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With