Skip to content

Instantly share code, notes, and snippets.

@lcpriest
Last active November 1, 2018 12:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcpriest/db74e742c632372479bee8a65af85825 to your computer and use it in GitHub Desktop.
Save lcpriest/db74e742c632372479bee8a65af85825 to your computer and use it in GitHub Desktop.
EmberJS dynamic query params

I was attempting to generate a list of links and found that some of them needed query params and others didn't. I could not find a recommended way to pass in a dynamic query-params object to the link-to helper. I ended up finding a solution in the Ember Discourse and decided to make it into an ember helper.

Helper:

import Ember from 'ember';

export function dynamicParams([routeName, params]/*, hash*/) {
  return [
    routeName,
    {
      isQueryParams: true,
      values: params
    }
  ];
}

export default Ember.Helper.helper(dynamicParams);

Usage:

{{link-to 'test-link' params=(dynamic-params routeName dynamicQueryParameters)}}

Where

let routeName = 'dashboard';
let dynamicQueryParameters = {
  superCoolMetrics: true,
  graphs: false
}

Will generate <a href="https://yourapp.com/dashboard?superCoolMetrics=true&graphs=false">test-link</a>

@srt4rulez
Copy link

If you use the block version of link-to, you'll need to omit the route as the first param, and just use it in the dynamic-params:

{{#link-to
    params=(dynamic-params routeName dynamicQueryParameters)
}}
    Test Link
{{/link-to}}

@Techn1x
Copy link

Techn1x commented Aug 2, 2018

Lifesaver, thanks! Don't know how this isn't officially supported in ember yet...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment