2

I'm using a backbone router to handle a client clicking various options on a single page. Among other things, this router also behaves similarly to simple, same-page anchor tag links.

The issue that I'm having is that if a user clicks one of the options (say, "details") then scrolls away, they may want to click "details" again. If they do so, nothing happens - the app has already routed to details and won't reroute. I would just use simple links, such as <a href="#details">Details</a>, but there is more going on than just jumping around the page. Is there a way to force the reroute to happen?

rybosome
  • 5,046
  • 7
  • 44
  • 64

3 Answers3

4

Building off Alexey's answer, it's possible to force Backbone's History / Router to "reroute" the current URL, as if it trigger a reload.

You can actually detect if the typical call to navigate fails / returns nothing due to already being on the same url, and call loadUrl to force it in those cases.

For example, my site-wide internal link handler looks like this:

// `Backbone.history.navigate` is sufficient for all Routers and will
// trigger the correct events. The Router's internal `navigate` method
// calls this anyways.
var ret = Backbone.history.navigate(href, true);

// Typically Backbone's history/router will do nothing when trying to load the same URL.
// In some cases (links with .allow-reload), we want it to re-fire the same route.
// We can detect when Backbone.history.navigate did nothing, and force the route.
if (ret === undefined && $link.hasClass('allow-reload')) {
    Backbone.history.loadUrl(href);
}
Community
  • 1
  • 1
philfreo
  • 41,941
  • 26
  • 128
  • 141
2

It will always return, there are no parameters to force it. I'm looking for the solution now and one that I see is silently replacing current route with new and then try to navigate to older one.

UPD: actually, you can use Backbone.history.loadUrl

Community
  • 1
  • 1
alice kibin
  • 572
  • 1
  • 4
  • 18
-1

If you bind the click event in your view, you can manually fire the route.

View.js

SearchView = Backbone.View.extend({
  initialize: function(options){
    alert("Alerts suck.");
    this.router = options.router;
  },
  events : {
    "click a.detail_link" : "showDetails"
  },
  showDetails : function(){
    this.router.navigate("/#details", true);
  }
});
Nick Lloyd
  • 51
  • 6