kramer65
kramer65

Reputation: 53843

How to use Backbone javascript router?

I'm trying to learn Backbonejs so I created my first route:

var Router = Backbone.Router.extend({
    routes: {
        "": "home"
    }
});
var router = new Router();
router.on("route:home", function() {
    alert('the router works!!');
});
alert('after the router!!');

I then navigated my browser to www.mydomain.com/thefolder/ which shows me the alert after the router!!, but unfortunately I don't get to see the router works!!. I tried inserting various routes, such as "/", "thefolder/" and "/thefolder/", but nothing seems to work.

Does anybody know what I'm doing wrong here?

Upvotes: 1

Views: 54

Answers (1)

nikoshr
nikoshr

Reputation: 33334

If I may quote the doc for Backbone.Router

During page load, after your application has finished creating all of its routers, be sure to call Backbone.history.start(), or Backbone.history.start({pushState: true}) to route the initial URL.

Add Backbone.history.start(); after your router declaration and you callback should be called.

See http://jsfiddle.net/nikoshr/wTU58/ for a demo

Upvotes: 4

Related Questions