Introduction to Query Params

Here, you’ll learn what query params are and how they are used in Ember applications.

We'll cover the following

Tuning

Our application looks decent, but currently, there’s no easy way to see a band’s highest-ranked song or find a particular song in a long list.

Next, we need to implement a feature to sort songs list and search the list with text the user has provided. Let’s also say we want to share the sorted, and possibly filtered, lists with our friends. The easiest way to do this is to reflect the sorting option and search term in the URL. This brings us to another Ember feature, query parameters. What are query parameters? Query parameters (QPs) are key-value pairs tacked onto the end of the URL. They’re very common in server-side programming, and they provide a simple way to modify what needs to be shown on the page and provide additional information that gets stored. Pagination is an example of the first task. If you go to the programming subreddit and click the “Next” button, the URL changes to this:

http://www.reddit.com/r/programming/?count=25&after=t3_2ou7md

Here, the count QP specifies how many posts should be shown on a page, while the after QP tells the server after which post the other twenty-five posts should be returned.

An example of the second use is a Google custom campaign’s query parameters, which tell Google how each visitor arrived at the page. This information is extracted from the URL and sent to Google Analytics.

Query parameters in Ember

After a couple of rewrites, Ember settled on a query parameter implementation that covered all use cases and was robust enough to be integrated into the framework.

There is a one-to-one mapping between the query parameter in the URL and the corresponding property on the controller. A binding is automatically set up between these two to keep them in sync.

Because each route has its corresponding controller, the list of query parameters can also be defined on the route. The concept of query parameters primarily relates to routing, we prefer to define them on the route.

You’ll see how to do that in a moment, so let’s add the ability to sort songs in the next lesson.

Get hands-on with 1200+ tech skills courses.