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
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
I would use onBeforeAction:
or onAfterAction:
.
E.g.
Router.route('/XXXX', {
name: 'someTemplateNme',
onBeforeAction: function () {
Session.set('currentPage', 'XXXX');
},
});
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’);
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
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!
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.
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:
I’m using your solution now, less code, looks better, works as well
Thank you for your help, much appreciated.
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)
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.
Isn’t better to just call Router.current().url
whenever you want in the client?
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