3

I tried to use backbone with a pushstate:

$(document).ready(function(){
    var App = Backbone.Router.extend({
        routes: {
            "/":            "homepage",
            "/questions":   "questions"
        },
        homepage: function() {
            console.log('why');
        },
        questions: function() {
            console.log('this is not working?');
        }
    });
    var the_app = new App();
    $("a").click(function(ev) {
        the_app.navigate( $(this).attr("href"), true);
        return false;
    });
    Backbone.history.start({pushState: true});
})

The problem is that whenever I click a link like one of this:

<a href="/"> home </a>
<a href="/questions"> questions</a>
<a href="/justlink"> just link</a>

Location in the browser changes without reloading, but the function, associated with it and which I already defined in routes, does not executes. Any idea what have I done wrong?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753

1 Answers1

4

From the fine manual:

extend Backbone.Router.extend(properties, [classProperties])

[...] Note that you'll want to avoid using a leading slash in your route definitions:

You want your routes to look like this:

routes: {
    "":            "homepage",
    "questions":   "questions"
}

Demo: http://jsfiddle.net/ambiguous/ybPcg/

mu is too short
  • 426,620
  • 70
  • 833
  • 800