I currently have a routes object that has several regular pages, and would like also to add a top-level route in which the first parameter is a username.
routes : {
'/' : 'main',
'/login' : 'login',
'/about' : 'about',
'/create' : 'create',
'/:username': 'getUser' //something like this
'*actions' : 'defaultHandler'
}
If I understand you correctly, you're there. Just add username
as the parameter of your getUser
function:
getUser: function(username) { /* do stuff */ }
There's a problem, though, because your last two routes both can match just about anything. In order to identify users, you might use a prefix to differentiate user routes from everything matched by your final splat. Something like this should suffice:
routes : {
// ...
'/users/:username': 'getUser'
}
What happens when you have a user called "about" or "create"? Namespace your routes and avoid the problem:
routes : {
'/' : 'main',
'/a/login' : 'login', // "a" for "application"
'/a/about' : 'about',
'/a/create' : 'create',
'/a/*actions': 'defaultHandler',
'/:username' : 'getUser'
}
Then you just have to make sure no one has "a" as a username. This approach also protects you against future conflicts if you add more routes.