ember.js路由层次结构

时间:2012-10-17 16:43:20

标签: ember.js ember-old-router

我想要路由层次结构。 因此,如果URL“#/ user / 123”和“#/ user / 123 / albums”能够正常工作,那就太好了。 我想这样做:

App.Router = Em.Router.extend({
  root: Em.Route.extend({
    index: Em.Route.extend({
      route: '/'
    }),
    user: Em.Route.extend({
      route: '/user/:login',
      connectOutlets: function(router, context){
        router.get('applicationController').connectOutlet('user', context);
      },
      albums: Em.Route.extend({
        route: '/albums',
        connectOutlets: function(router){
          router.get('applicationController').connectOutlet('albums');
        }
      })
    })
  })
});

不是这样的:

App.Router = Em.Router.extend({
  root: Em.Route.extend({
    index: Em.Route.extend({
      route: '/'
    }),
    user: Em.Route.extend({
      route: '/user/:login',
      connectOutlets: function(router, context){
        router.get('applicationController').connectOutlet('user', context);
      }
    }),
    albums: Em.Route.extend({
      route: '/user/:login/albums',
      connectOutlets: function(router){
        router.get('applicationController').connectOutlet('albums');
      }
    })
  })
});

我的第一个代码考试模拟是愚蠢的,但我希望有路由层次结构。有没有能力做到这一点?!

1 个答案:

答案 0 :(得分:1)

这种事情肯定是可行的。嵌套路由有很多例子。我无法针对一个真实的应用程序检查您的特定路由器,但在Ember Application Structure guide中有一个很长的例子。请参阅嵌套部分。你会发现这个:

post: Ember.Route.extend({
  route: '/posts/:post_id',
  connectOutlets: function(router, post) {
    router.get('applicationController').connectOutlet('post', post);
  },
  index: Ember.Route.extend({
    route: '/',
    redirectsTo: 'comments'
  }),
  comments: Ember.Route.extend({
    route: '/comments',
    showTrackbacks: Ember.Route.transitionTo('trackbacks'),
    connectOutlets: function(router) {
      var postController = router.get('postController');
      postController.connectOutlet('comments', postController.get('comments'));
    }
  }),
  trackbacks: Ember.Route.extend({
    route: '/trackbacks',
    showComments: Ember.Route.transitionTo('comments'),
    connectOutlets: function(router) {
      var postController = router.get('postController');
      postController.connectOutlet('trackbacks', postController.get('trackbacks'));
    }
  })
})