11

I am trying to pass query parameters in Router.go like below:

var filter = 'abc';
var path = Router.current() && Router.current().path;
Router.go(path, {query: {filter: filter}});

But this does not change the url, it stills loads current path without query string only. But if I add the query parameter manually to path like:

Router.go(path+'?filter='+filter);

this works fine. But since I am trying to load same page with some filtered data. So clicking filter button repeatedly appends the filter string again and again to path.

What is the correct way of passing query string using iron router?

Aashu Agarwal
  • 173
  • 1
  • 2
  • 8

4 Answers4

12

Right there in the docs

Router.go('post.show', {_id: 1}, {query: 'q=s', hash: 'hashFrag'});

The above JavaScript will navigate to this url:

/post/1?q=s#hashFrag

https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#named-routes

ChatGPT
  • 5,334
  • 12
  • 50
  • 69
4

The parameters are Router.go(path, params, options). The query part should go in the options parameter, so try the following: Router.go(path, {}, {query: {filter: 'filter='+filter}}).

EDIT

Answer updated according to Robins comment below.

Peppe L-G
  • 7,351
  • 2
  • 25
  • 50
  • I tried this but page does not re load with query parameters – Aashu Agarwal Sep 02 '14 at 10:51
  • 6
    This answer is almost correct. query needs to be a string like `Router.go(path, {}, {query: "filter=filter"})`. From Iron Router source line 220 https://github.com/iron-meteor/iron-router/blob/devel/lib/router_client.js – Robin May 28 '15 at 18:13
  • Hehe, a 5 times up voted answer, and it's wrong ;P But good catch, that should explain why it didn't worked for Aashu. – Peppe L-G May 29 '15 at 06:15
3

I found that if your first parameter in Router.go is a path, instead of a template name, the query filter is not passed. Use a template name:

Router.go(templatename, {_id: 1}, {query: 'q=s', hash: 'hashFrag'}); 
ekad
  • 14,436
  • 26
  • 44
  • 46
dpatte
  • 69
  • 1
  • 7
1

Try this:

var path = Router.current() && Router.current().route.originalPath;

That should give you the path without the query string attached.

richsilv
  • 7,993
  • 1
  • 23
  • 29