2012-07-08 67 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

我覺得這個問題已經解決了。我正在使用沒有索引的餘燼路由,不面臨任何葉狀態問題。我一直在瀏覽爲什麼我們需要一個索引,而我在這裏登陸。是否還有其他原因使用索引狀態?