How to refresh a page in a backbone application

Zhen picture Zhen · Jan 17, 2012 · Viewed 40.6k times · Source

I am using backbone to build my web app.

Currently I am facing an issue whereby if I am on the home page, I am unable to refresh the same page by just clicking on the 'home' button again.

I believe that this is the limitation provided by backbone (does not reload the page if the same URL is called)

enter image description here

Is there any way around this? So that I can trigger a page reload when I click on the home button again i.e. call the same URL again?

Answer

amiuhle picture amiuhle · Jul 23, 2013

You're looking for Backbone.history.loadUrl. From the Annotated Source:

Attempt to load the current URL fragment. If a route succeeds with a match, returns true. If no defined routes matches the fragment, returns false.

So, for a simple refresh link, you can add the following event to your Backbone.View:

events: {
  'click a#refresh': function() {
    Backbone.history.loadUrl();
    return false;
  }
}