4

I've been facing this issue with Backbone routing and figured I'd spent enough time investigating:

There are two urls at play here: / and /post/:id. The / page has links to various posts via /post/:id. When I click the post link, the post page loads, but backbone immediately changes the url to /. Not only does this look bad, it also triggers route handlers at the wrong time. I'm not doing anything special... here's my code:

PostRouter = Backbone.Router.extend({
        routes : {
            "" : "doHome"
        },
        initialize : function() {

        },
        doHome : function() {
                    // do some stuff before navigating
            window.location = "/";
        }
})

...

var router = new PostRouter();
Backbone.history.start({ pushState: Modernizr.history });

Again, the doHome function is called immediately after the post page loads. Clearly this causes the site to navigate back to the home page. I can obviously remove that call to window.location to prevent that, but the url still gets updated to the root url, which isn't acceptable.

Thanks in advance!

UPDATE 1:

If I go directly to "localhost:808/post/:id" the url immediately changes to "localhost:8080/". However, if I do this exact same thing in private browser window, this behavior is not observed.

UPDATE 2:

Given what I found in update 1, I went crazy and started from scratch: I cleared 4 weeks of browsing history (sigh), stopped my local server and cleaned up all persistent sessions and redeployed my app. Alas, it worked! That said, I am not listing this as a solution as it doesn't help explain what exactly is going on and how to solve it. Additionally, it leaves me concerned about this happening to users of my site. I'd have no way to tell that this was happening and, even if I did, I couldn't tell them how to fix it on their end (clearing 4 weeks of browser history is not an option!). Can anyone shed some light on what might have been going on?

threejeez
  • 2,314
  • 6
  • 30
  • 51
  • You don't seem to have a route defined for /post/:id. Is that truly the case, or just an error in posting the question? – Brian Reischl Aug 13 '12 at 19:03
  • It is truly the case. The /post/:id links are actually hard links that go directly from / to the post page. The post page is, in fact, /post/:id, so it doesn't need to handle the route. Perhaps the confusion is that I used backbone nomenclature when describing the post url. – threejeez Aug 13 '12 at 19:11
  • `Router` is not gonna work if you haven't instantiate it with a `var myRouter = new PostRouter()`. I don't think is the cause of the issue but it worths to be mentioned. – fguillen Aug 13 '12 at 19:58
  • It's instantiated in the ... ;). I'll add it to the code above. – threejeez Aug 14 '12 at 00:36
  • I think you should fix your code example because as I see it it will reproduce a recursive behavior due you start your application in `/` and the `doHome` handler will redirect the page to `/` again through the `window.location`. – fguillen Aug 14 '12 at 09:36

1 Answers1

0

why don't you try to add

console.log(Backbone.history.handlers);

at the end to see, how your rout is added to Backbone.history. This might shed some light.

schacki
  • 9,401
  • 5
  • 29
  • 32