I have a couple of places in my application where I add some query-params to a {{link-to}} helper. They have gotten quite unwieldy and ‘hardcoded’ for example:
This might be used a few times on a single page so I thought I would create a helper that made all of the params null unless I passed in the desired namedArgs.
I created the helper but I can’t seem to add it without generating an error. For example I have tried:
however both create errors and stop the page from loading. I have used custom helpers elsewhere by just using {{helpername}} but that was in the template, not in another component.
One thing to note is that the query-params helper is simply an object that has isQueryParams: true. So you can make your own helper and use it in place.
In future it might check the object type (last time I looked this class never existed, it was just but for now it is just checking isQueryParams.
Here is a helper I use so that the router clears all params for the link.
import { helper } from '@ember/component/helper';
import Object from '@ember/object';
import { assign } from '@ember/polyfills';
export function explicitQueryParams(params, hash) {
let values = assign({}, hash);
values._explicitQueryParams = true;
return Object.create({
isQueryParams: true,
values,
});
}
export default helper(explicitQueryParams);
I then use the property I have set here in the router to clear them, will paste just so the example is complete, however it isn’t relevant to what you want.
In my case this means any previous query params are cleared when clicking this link whereas the default (read: annoying) behaviour is to add any current params to that link for some reason.
Your solution actually suits me down to the ground. I didn’t want to switch off the sticky params because in most cases they suit the use case for my app but on these particular templates they cause a problem. The ability to switch the sticky params off on link to link basis is superb.
I actually wonder if that shouldn’t be a default helper.
I would try to pass to the link-to an object as queryParam built from the controller as a computed property where you get the queryParams array setting all properties to null value except the ones you want, also you can make the same with an action which does the same through the transitionToRoute method.
There are old issues about query params and they even ignored bugs with the excuse of “but routeable components”. I think I still have mixins fixing bugs. In discussions though the query params are meant to be reworked and improved a lot I think, no idea when though.