Search code examples
javascriptjquerybackbone.jsrouter

My backbone js router isn't working


I am just starting to learn backbone js. I am not understanding the router. I have written a simple code. What it should do is to print 'I am working' in the console. But it is not working.

$(document).ready(function(){
var AppView = Backbone.View.extend({
    el: 'body',
    initialize: function(){
        console.log('this is working');
    }
});
var AppRouter = Backbone.Router.extend({
    routes: {
        '': function(){
            var a = new AppView();
        }
    }
});
    var ar = new AppRouter();
Backbone.history.start();
});

Solution

  • You almost certainly have a Backbone version problem. The 1.0.0 Change Log entry says:

    • The routes in a Router's route map may now be function literals, instead of references to methods, if you like.

    And you can even find the trivial one line change in the revision history that made this new behavior possible.

    Older versions of Backbone wanted route method names in routes and didn't know what to do with functions. Upgrade to the most recent Backbone (and Underscore and jQuery) and your code will work.

    Demo: http://jsfiddle.net/ambiguous/sfeCr/1/