Assuming you route is declared as the following:
routes : {
'/user/:id' : 'user'
}
Your initialize
code is not working because when you initialize your router with a url such as: /user/1234
. Backbone.history.fragment
will be /user/1234
(not /user/:id
). Since the this.routes
object doesn't have a key of /user/1234
, your else
clause calls the default
route.
If you first instantiate your router
then call Backbone.history.start()
, you will be able to remove your router initialize
code. When you navigate to a url as /user/1234
your router will match the /user/:id
route and call the user
function.
The following should work for you without adding your initialize
code:
var router = (app.user.role === 'manager') ? new routers.manager()
: new routers.guest();
Backbone.history.start({pushState: true, hashChange: false});