Backbone : navigating without pushing to history

October 16th, 2012 § 0 comments

Recently I came across a situation that I need a backbone route just for redirect with some parameters. I easily done with app.navigate method, but later I noticed a issue that I can’t use back button after the redirect since when ever I press back button it goes to the redirect initialization route and it will redirect to the same page.

Lets take an example of my routes /#redirect and /#destination?a=1&b=2 . So when ever I reach /#redirect it will simply navigate to /#destination?a=1&b=2. When ever I press back button from /#destination?a=1&b=2  it reaches /#redirect and that will redirect back me to the same  /#destination?a=1&b=2, which means back button from /#destination?a=1&b=2  is not at all effective. So I need backbone to skip pushing /#redirect to its history stack.

So here is how you can ask backbone to replace /#redirect from history stack with /#destination?a=1&b=2 so pressing back button from /#destination?a=1&b=2 will never go to /#redirect. You can pass and optional parameter replace: true to navigate method to do this.

In this trigger: true will fire the navigation to the route and replace: true will replace the previous route in the history stack.

Remember that this will work only in modern browsers which works with pushState.

Thank you.

Revath S Kumar [RSK]

- Rubyist / JavaScripter - Yeoman Team member - Open source enthusiast - Blogger @ PHPRepo and - Coder @ Whatznear - Google fanboy - jQuery lover

More Posts - Website - Twitter - Facebook - LinkedIn - Pinterest - Google Plus - YouTube