1

I'm having a headache with Backbone router in Safari

I have this piece of code:

app.navigate("ask/" + encodedSearchKey,true);

and in my router:

var AppRouter = Backbone.Router.extend({

  routes:{
    "":"main",
    "ask/*encodedSearchKey":"askSearch",
  },

  askSearch:function(){
    ...
  },
  ...

});
app = new AppRouter();
Backbone.history.start();

In chrome, it works as expected, the URL gets routed and the askSearch function gets called once

however, in Safari, the askSearch function actually gets executed twice

and when I tried

app.navigate("ask/" + encodedSearchKey,false);

In chrome the askSearch function was not called as expected, but the Safari it actually gets called once

I have run through the debugger and am pretty sure the app.navigate line always gets called once only, and there is nothing else that could fire the askSearch function except the router itself

now I know I can fix this by detecting browser type, but I did not find any similar problems online, it seems that people do not have this issue, am I doing something very wrong here?

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
Matthew Yang
  • 605
  • 1
  • 13
  • 23

3 Answers3

1

I think I've just run across the same issue and found a solution... Safari seems to strip the string from the url for vanity purposes, this then re-triggers the router causing the view to rendered again but lacking the query string data.

The solution I used was fairly simple in the router:

myView: function () {
    if (window.location.search.length) {
        myView.render();
    } else {
        return false;
    }
}

This stops the 2nd render from proceeding. Although this os only really applicable if you only ever want to render that view with a query string.

Merlin Mason
  • 1,607
  • 2
  • 12
  • 14
0

The navigate function's second argument is not a boolean but an option object:

app.navigate("ask/" + encodedSearchKey, {
    trigger: true
});
Tal Bereznitskey
  • 2,051
  • 1
  • 20
  • 20
  • This answer is wrong. You can pass a bool as the second param as a shortcut to set trigger. I don't know why this would be accepted. – DriverDan Aug 16 '13 at 17:23
0

in Firefox is the url is not encoded the rout will fire twice, some times...

Zeev G
  • 2,191
  • 21
  • 37