I have the following router defined, but the .route function doesn’t seem to be setting. What am I doing wrong? Thanks, in advance, for the help.
# app.js.coffee
initialize: =>
router = new Backbone.Router
router.route "foo/:bar", "baz"
console.log router.routes # returns undefined
The routes you create using
Router.routeare stored internally in theHistoryobject — they’re not added to theRouter.routescollection.They still work though, see here for proof. Note that in
this.routes, only thehomeroute is defined, but you’re still able to hit thebazroute. You can see thebazroute if you checkBackbone.history.handlers, which is where the routes are actually stored.
Note though, I think you need to use
thisin your code, becauserouterwon’t be defined yet insideinitialize: