Ember 2: Getting an object out of query params in a URL


#1

In Ember 2, for a URL with a query string of ?q[a]=1&q[b]=2, how do I get those params in the controller?

I know how to do it with a single string (q=foo) or an array (q[]=1&q[]=2), but for the above, I can’t make it work.


#2

I am not exactly sure what the question is. When you have “page=4&query=test”

http://bloggr.exmer.com/posts/7DA714FF-9B78-49A4-96A2-ED4ABCC0D4C4?page=4&query=test

You can still get those parameters on the controller in Ember 2 like this:


#3

Yes, if the URL has this:

?foo=1&bar=2

I can get the data as two strings like this:

foo: null,
bar: null,
queryParams: ["foo", "bar"]

Then if the URL is like this:

?values[]=1&values[]=2

I can get the data into an array of two values like this:

values: []
queryParams: ["values"]

But if I have the following the URL:

?values[foo]=1&values[bar]=2

How do I get the data into an object like the following?

{ "foo": "1", "bar": "2" }


#4

Something like this has worked for me:

queryParams: ["values"],
values: [],
actions: {
  setParam(){
    const values = {
      foo: "1",
      bar: "2"
    }
    this.set("values", values);
  }
}

Note: You can’t initialize values as an object directly, but if you initialize it as an array, and then set it with an object, it will hold it in the format you described (eg ?values[foo]=1&values[bar]=2)