Backbone: Refresh the current route

There are several things that would accomplish this. If you anticipate navigating to a route that is already in the url, then first check the current route fragment with Backbone.history.fragment and see if it is the same as the route you want to activate. If so, then you can do any of the following:

  1. You could just call the route method directly: sys.view1();

  2. You could set the fragment to another dead route, then back to the route you want.

    sys.navigate('someDeadRoute'); sys.navigate('view1',{trigger: true});

  3. You could stop and restart the router:

    Backbone.history.stop(); Backbone.history.start()

    This would pickup the route again and run it.

I think I would go with #1.


Rather than starting & stopping Backbone's history, you can call Backbone.history.loadUrl.

To refresh the current page:

Backbone.history.loadUrl(Backbone.history.fragment);

Or as a link handler:

// `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.
// But since 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) {
    Backbone.history.loadUrl(href);
}

Tags:

Backbone.Js