0

現在、いくつかの通常のページを持つルート オブジェクトがあり、最初のパラメーターがユーザー名であるトップレベルのルートも追加したいと考えています。

routes : {
  '/'        : 'main',
  '/login'   : 'login',
  '/about'   : 'about',
  '/create'  : 'create',
  '/:username': 'getUser' //something like this
  '*actions' : 'defaultHandler'
}
  1. どうすればこれを達成できますか?
  2. ページまたはユーザーが見つからないときにアクションにフォールバックしながら、ユーザー名ルートを追加するのは悪い設計でしょうか?
4

2 に答える 2

1

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'
}
于 2012-04-13T02:49:02.773 に答える
1

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.

于 2012-04-13T02:52:45.217 に答える