Search code examples
meteoriron-router

How to set querystring or parameters dynamically with iron-router?


I've seen this thread on how to append/modify query parameters without duplicating, but I can't make it work.

I have a page in my Meteor application that lists items and allows filtering. I would like the filtering to be URL-based, so every change to a filter should reflect in the URL. I use iron-router to then update the subscription accordingly.

For some reason, I can't modify the querystring though. This code doesn't trigger any action if called from a Template.templateName.events(...):

# valuePairs = ["foo=bar", "bar=foo"]    
newUrl = Router.current().route.path {}, { query: valuePairs.join('&') }
Router.go newUrl

But if I call Router.go(newUrl) from the commandline, the expected result happens, i.e. the URL updates.

Is there another, preferred method to modify the query?

If not, how can I make above code work?

UPDATE - I just found that after running the event, if I go back in my browser history I can see the correct URL. It looks like something (?) triggers another page load to go back to original page.


Solution

  • Just based on

    if I go back in my browser history I can see the correct URL

    Do you suppress the actual link event? An <a href='#'> would flip flop you right back.

    Template.tempyTheTemplate.events({
        'click awesomeButton': function() {
            // URL stuff
            Router.go(magicURL);
            return false;  // <-- suppresses the event
        }
    });