McVenco
McVenco

Reputation: 1009

How to go to last visited Meteor route after clearing real-time/reactive search input?

In my website, I have a top navigation bar with a search field. Once I enter more than 2 characters in the search field, iron-router takes me to my 'search' route and displays the results. When I remove the input from my search field, I remain in my search route (displaying no results), but I want to change this so that I will return to the last page that I visited (where I started typing in the search field).

My body contains this routing:

  <div id="page-wrapper" class="gray-bg">
    {{> topNavbar }}
    {{> yield}}
    {{> yield 'bottom'}}
  </div>

In my topnavbar.js I have the following code that takes me to the search results:

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')
    }
});

I know that I can get the current route with Router.current().route.getName(), but because the search input updates the route everytime I type a character this isn't very useful...

I have tried this code in my topnavbar.js to log the route to the console:

Router.onStop(function () {
    Session.set("previousLocationPath", Router.current().route.getName());
});


Router.onBeforeAction(function () {
    var previousLocationPath = Session.get("previousLocationPath");
    console.log(previousLocationPath);
    this.next();
});

Right now I'm quite stuck in my thinking path what I can do to return to my previous, non-search route. I'm still pretty much a novice with Javascript, so I might miss something obvious, so I was hoping someone could point me in the correct direction...

Thanks in advance!

Upvotes: 1

Views: 86

Answers (1)

MatiK
MatiK

Reputation: 573

I think you can just use plain JS history.back() to go back to previous route as iron-router uses history api for routing.

Upvotes: 1

Related Questions