Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Query Params unset Query Parameter

Tags:

ember.js

I am using the new Ember Query Parameters and having issues "unsetting" a query parameter.

I have a scenario where I need to navigate from something like: ?game=13 to ?question=14. This means that I have 2 query parameters on my router: game, question. Unfortunately this transition (coming from ?game=13):

this.transitionToRoute({queryParams: {question: 14}} );

This unfortunately leads to :

?game=13&question=14.

I have also tried:

this.transitionToRoute({queryParams: {question: 14, game: null}} );

which leads to:

?game=null&question=14

Because somehow everything is stringtransformed.

How can I transition to ?question=14 and remove the game query parameter?

like image 475
m0c Avatar asked Jul 31 '14 22:07

m0c


People also ask

What is a query parameter in Ember?

Common use cases for query params include representing the current page number in a paginated collection, filter criteria, or sorting criteria. In web development, query parameters are used within a URL as described above but can also be used in API requests that retrieve data. Ember treats these as two different concepts.

What are query params?

Query parameters are optional key-value pairs that appear to the right of the ? in a URL. For example, the following URL has two query params, sort and page, with respective values ASC and 2:

What happens when you change the query parameter in transitionto?

When you change query params through a transition ( transitionTo and <LinkTo /> ), it is not considered a full transition. This means that the controller properties associated with the query params will be updated, as will the URL, but no Route method hook like model or setupController will be called.

Why is my query Param not serializing to the URL?

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.


1 Answers

Initialize your properties storing query params to some value. Set them back to this initial value to remove them from your URL later:

App.SomeController = Ember.Controller.extend({
    queryParams: ['myParam'],
    myParam: "initial",

    ...
);

Later...

controller.set('myParam', "initial");
like image 123
kaftz Avatar answered Nov 04 '22 19:11

kaftz