Salvador Dali
Salvador Dali

Reputation: 222929

backbone router does not pick up the function

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?

Upvotes: 3

Views: 1091

Answers (1)

mu is too short
mu is too short

Reputation: 434945

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/

Upvotes: 4

Related Questions