I’m searching for a possibility where I can use Router.current().route.getName() as an alternative way to return to the previous page. I know I could use Javascript’s history.back() function, but I’d like to know how I can use Meteor for this, just for the sake of using Meteor
I have a navbar with a reactive search field on my website. When I start typing in the search field, iron-router immediately takes me to the ‘search’ route and displays the search results. However, when I clear the search field I’d like to return to the page where I was before I started the search.
Code in the html body:
<div id="page-wrapper" class="gray-bg">
{{> topNavbar }}
{{> yield}}
{{> yield 'bottom'}}
</div>
My top-navbar.js contains the following:
Template.topNavbar.events({
'keyup input': function (e, t) {
var s = t.find('input').value;
if (s && s.length >= 2) {
Session.set('searchtext', s);
Meteor.subscribe('vod-search', s);
} else {
Session.set('searchtext', '');
Meteor.subscribe('vod-search').stop();
}
Router.go('search')
}
});
Now I have started playing around with the following, storing the current url in a variable using Router.current().route.getName(), but I’m a bit clueless how I can access this variable from within the Template.events, or whatever else I can do to make this work
Template.topNavbar.rendered = function(){
Router.onRun(function () {
Session.set("previousLocationPath", Router.current().route.getName());
var previousLocationPath = Session.get("previousLocationPath");
//console.log(previousLocationPath);
this.next();
})
};
What I eventually want to achieve is something like this:
Template.topNavbar.events({
'keyup input': function (e, t) {
var s = t.find('input').value;
if (s && s.length >= 2) {
Session.set('searchtext', s);
Meteor.subscribe('vod-search', s);
Router.go('search')
} else {
Session.set('searchtext', '');
Meteor.subscribe('vod-search').stop();
Router.go(previousLocationPath) // I don't actually know if this is possible, but I hope you get the idea...
}
}
});
Any help is greatly appreciated!