9

My current backbone application has a url:

localhost/#users

Is there a way to access localhost/#users while at the URL localhost/#users so it refreshes the page?

Currently, when I am at localhost/#users and I try

window.location.hash = #users or myBackboneRouter.navigate("users")

it does not trigger a page refresh.

kidcapital
  • 5,064
  • 9
  • 46
  • 68
  • 1
    Why do you want to reload the page that the user is already on? – Edward M Smith Mar 29 '12 at 20:25
  • 1
    I have some ajax bindings that I want to run again on the same page. – kidcapital Mar 29 '12 at 21:38
  • Not sure I follow what you mean by "ajax bindings", but can you just make a function to call them again, then call that function instead of routing? Seems rather heavy handed to re-render the entire page to refresh some smaller part of it. – Edward M Smith Mar 30 '12 at 03:01

4 Answers4

35

To refresh same page in backbone, you have to use

Backbone.history.loadUrl(Backbone.history.fragment);
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
6

I think myBackboneRouter.navigate("users", {trigger: true}) will do what you want.

heavi5ide
  • 1,599
  • 10
  • 11
  • I tried this but it still won't trigger it :( – kidcapital Mar 29 '12 at 21:38
  • 8
    Ah okay. Yeah, I see `if (this.fragment == frag) return;` in the navigate function, so I guess that's just short-circuiting the process before trigger would force it to call the route. If you just want it to run the code that runs when you land on that route, just manually call the function that the router routes that hash to. For example, if your router has `routes: {'users': 'users'}` then you can just call `myBackboneRouter.users();` you don't have to call navigate. – heavi5ide Mar 29 '12 at 21:49
  • 2
    I upvoted the answer but heavi5ide's comment was the real solution for me. – jason.zissman May 03 '13 at 16:00
  • In backbone 1.0, it reads `this.fragment === frag`. It's possible to cast the frag passed in a unique String object and force this comparison to be false. – Elf Sternberg Oct 07 '13 at 20:25
2

I use these three line of codes to reload my backbone page:

router.navigate(Backbone.history.fragment, true);
Backbone.history.loadUrl( Backbone.history.fragment );
router.refresh(true);

OR Simply

Backbone.history.loadUrl(Backbone.history.fragment);
Neel Shah
  • 791
  • 1
  • 6
  • 17
  • Where would I need to put this in my router? In ever route/when the route is instantiated/somewhere else? – Anna Nov 10 '15 at 23:12
-1

Why wouldn't you use

window.location.reload();

Either that or recall your view render() function..

Ronan
  • 1,482
  • 11
  • 11
  • 1
    The problem with window.location.reload() is that it reloads the entire page. This is undesirable if you want your Backbone router to control what is re-rendered without having to reload the entire page. – NoBrainer Sep 21 '15 at 18:49