Backbone Router not working for direct access
backbone.js
Solution
You start `Backbone.history` before you set up your listeners, which means they can't react to the initial routing that occurs on page load.
Move `Backbone.history.start();` out of your router definition, or at least after you setup the routes, and you should be good to go.
var appRouter = Backbone.Router.extend({
routes: {
"blog": "blog",
"*other": "defaultRoute"
},
blog: function() {},
defaultRoute: function() {}
});
app.Router = new appRouter();
app.Router.on('route:defaultRoute', function() {
console.log("Default Route");
});
app.Router.on('route:blog', function() {
console.log("Blog Route");
});
Backbone.history.start();
And a demo http://jsfiddle.net/nikoshr/k3Lcxwq2/
Problem
My backbone router works fine if you click a link but doesn't work when trying to access a URL directly or if you refresh the page. Router ``` var app = app || {}; var appRouter = Backbone.Router.extend({ routes: { "blog": "blog", "blog/post/:id": "blogPost", "faqs": "faqs", "*other": "defaultRoute" } }); app.Router = new appRouter(); Backbone.history.start(); ``` App ``` var app = app || {}; var view = new app.AppView(); app.Router.on('route:defaultRoute', function() { console.log("Default Route"); } app.Router.on('route:blog', function() { console.log("Blog Route"); view.showBlog(); } ``` I log to the console whenever a route is fired, but when you access a url directly or when refreshing the page no route is fired, not even the default route.