2012-07-08 28 views
6

我最近注意到Ember Router的一些东西,它只允许导航到叶子路线 - 没有子路线的路线。为什么Ember路由器只允许导航到叶子路由?

现在,除非我不正确地做事,否则这看起来像是设计中的错误/错误。

让我们举个例子是这样的:

我有项目的集合,每个项目都有很多的合作者,有了这个,我想建立一个UI有一个3栏布局(像您的标准的桌面电子邮件客户端)在左边我有一个项目列表,当点击一个项目时,中间一列显示了一个协作者列表,点击一个协作者将其详细信息加载到右侧列中。

现在,在点击一个项目时,我想导航到/projects/1,点击一个协作者时导航到/projects/1/collaborators/23

这里是说明嵌套路线的第一部分路由器:

App.reopen(
    Router: Ember.Router.extend(
    enableLogging: true 
    location: 'hash' 

    root: Ember.Route.extend(
     index: Ember.Route.extend(
     route: '/' 

     redirectsTo: 'projects' 
    ) 

     projects: Ember.Route.extend(
     # This route is not routable because it is not a leaf route. 
     route: '/projects' 

     connectOutlets: (router) -> 
      # List projects in left column 
      router.get('applicationController').connectOutlet('projects', App.projects) 

     show: Ember.Route.extend(
      # This route is routable because it is a leaf route. 
      route: '/:project_id' 

      connectOutlets: (router, project) -> 
      # Render the project into the second column, which actually renders 
      # a list of collaborators. 
      router.get('projectsController').connectOutlet('project', project) 
     ) 
    ) 
    ) 
) 
) 

正如你会看到余烬不会调用updateRoute(设置URL),直至过渡到root.projects.show,因为这条线的https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/routable.js#L81

有没有其他人做过这样的事情?有没有更好的方法来设计这个?

回答

7

我发现要做到这一点的最佳方式是使用路由“/”的root.projects.index状态,而没有别的。这样每个页面都有自己特定的状态。

projects: Ember.Route.extend(
    # This route is not routable because it is not a leaf route. 
    route: '/projects' 

    connectOutlets: (router) -> 
    # List projects in left column 
    router.get('applicationController').connectOutlet('projects', App.projects) 

    index: Ember.Route.extend(
    route: "/" 
) 

    show: Ember.Route.extend(
    # This route is routable because it is a leaf route. 
    route: '/:project_id' 

    connectOutlets: (router, project) -> 
     # Render the project into the second column, which actually renders 
     # a list of collaborators. 
     router.get('projectsController').connectOutlet('project', project) 
) 
) 

N.B.这就是说我正在做一个3列布局的类似的事情,并且将中间和右列与上面的路线匹配,并将共享布局中的左列添加到每个可能的中间视图。

+0

谢谢布拉德利,这似乎是伎俩。 – Ivan 2012-07-08 07:55:57

+0

非常聪明。谢谢你的提示。 – nembleton 2012-07-08 10:33:24

+0

@BradleyPriest我对此有一个[问题](http://stackoverflow.com/questions/11377498/ember-js-crud-scenarios-specifying-view-from-within-a-route)。我发现很难理解如何在CRUD场景中使用Ember.Router。你可以看一下吗? – MilkyWayJoe 2012-07-08 11:22:58

2

我觉得这个问题已经解决了。我正在使用没有索引的余烬路由,不面临任何叶状态问题。我一直在浏览为什么我们需要一个索引,而我在这里登陆。是否还有其他原因使用索引状态?