Skip to content

Instantly share code, notes, and snippets.

@machty
Last active April 21, 2023 17:14
Show Gist options
  • Star 54 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save machty/8167051 to your computer and use it in GitHub Desktop.
Save machty/8167051 to your computer and use it in GitHub Desktop.
Ember Query Param API examples
@sarus
Copy link

sarus commented Nov 7, 2015

+1 on how to use objects in the query params (e.g., /book/5?filter[chapter]=Chapter1). This style is used heavily in json api and should be supported by Ember. Any ideas on how to go about it? It's also recommended for paging /books?page[number]=1&page[size]=100

@sarus
Copy link

sarus commented Nov 7, 2015

Just came up with a way to use object based query params which seems to work:

In the controller you need to map the controller property to your query param key:

queryParams: ['sort',{
    entityNameFilterValue: 'filter[entity.entity-name]'
  }],

Then in the routers model function you need to construct the params however you want based on the controller value:

model: function(params){
    if(params.entityNameFilterValue){
      params.filter = {};
      params.filter['entity.entity-name'] = params.entityNameFilterValue;
      delete params.entityNameFilterValue;
    }
    // do your store lookup now with params
}

@tchan
Copy link

tchan commented Jul 31, 2017

Thanks for this!

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