Using Router.current().route.getName() as an alternative history.back option?


#1

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 :wink:

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!


#2

I would not go to the search route, but would just add something like ?search=searchString to URL which will reflect current search string.
Without that paramter yield normal template, if it is present - yield search results.
PS: flow-router have nice API for that


#3

I’d rather keep iron-router in place. I’m working on an already pretty large app and changing the router means I have to change a lot more than just this piece of code :smile:

Also, the search route leads to a separate search template (html page) so that would require some rewriting too… I was just trying to figure out if there would be a way to do this, other than using history.back()


#4

Seriously, it will only take an afternoon, or maybe two in the worst-case scenario, to get rid of iron-router and switch to flow-router. You’ll never look back and your app will be more stable and your development process more sane for the entire remainder of the lifetime of your app. It’s a no-brainer. Get with the program, please.


#5

Easy now, I’m still pretty new to Meteor (or web development, for that matter), I’m still mostly figuring things out…
I’ll suggest it to the team (or just try it myself on a separate branch first…), thanks anyway!