Help with Iron Router


#1

Hello!

I’m trying to figure out how i can use iron router (or something else?) to get this:

when a user goes to mypage.com/XXXX

i want this to happen

Session.set('currentPage', 'XXXX');

Is this possible?

br
Glutch


#2

I would use onBeforeAction: or onAfterAction:.

E.g.

Router.route('/XXXX', {
    name: 'someTemplateNme',
    onBeforeAction: function () {
       Session.set('currentPage', 'XXXX');
    },
});

#3

Hmm let me rephrase my problem.

If the user types mysite.com/whateverintheworld
i want to set Session.set(‘currentPage’, ‘XXXX’);
to whatever the user typed in the adressbar (after the slash)

So if the user types mysite.com/randomword123
I want Session.set(‘currentPage’, ‘randomword123’);


#4

Modifying chompomonim’s response:

Router.route('/:someValue', {
    name: 'someTemplateNme',
    onBeforeAction: function () {
       Session.set('currentPage', this.params.someValue);
    },
});

Using a colon : in the path tells the router to expect a variable value there, which can then be accessed as a key in the this.params object, with the value that was in place of that key in the path.

Have a look at the iron router docs for more details: https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#route-parameters


#5

Thank you! It works nicely

How can i change mysite.com/(thisthing) without refreshing the page?

Inside this one

Template.navigate.events({
    'submit form': function(event){
        event.preventDefault();
        var board = event.target.gotoBoard.value;
        Session.set('currentPage', board);
        document.title = board;
        /*please change the address bar to mysite.com/(variable board) without refreshing the page)*/
    }
});

EDIT:
I found a solution with pure javascript

window.history.pushState('Object', 'Title', variable);

Sweet!


#6

Again I’m going to point you back to the iron router docs, they’re very helpful. I very much recommend taking a look through if you find yourself using iron router a lot.

Iron Router Navigation

But more simply:

Template.navigate.events({
    'submit form': function(event){
        //your other code
        Router.go('/yourpath');
    }
});

If you’re already using iron router (heck, Meteor) I wouldn’t recommend this:


#7

I’m using your solution now, less code, looks better, works as well :smile:

Thank you for your help, much appreciated.


#8

Sorry for spamming, but a quick question.

Would this be the correct way to do it using a variable? (It works)

Router.go(’/’+variable);

(Couldn’t find anything about variables in the docs, sorry)


#9

Yes, as far as I can tell. The first parameter can be either a path (as above) or the name of the route, if you supplied it where you defined your routes. I believe either way it has to be a string.

And don’t worry, asking questions isn’t spamming.


#10

Isn’t better to just call Router.current().url whenever you want in the client?


#11
Router.route(':page', {
  name: 'home',
  onBeforeAction: function() {
    Session.set('currentPage', this.params.page);
  }
});

To go to another page use

Router.go('home', {page: 'users'});

This will make you go to /users and make Session.get('currentPage'); return users