Search code examples
ember.jsember-old-router

ember.js routing hierarchy


I would like to have routing hierarchy. So it would be nice if URLs "#/user/123" and "#/user/123/albums" will work. And i want to do that this way:

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');
        }
      })
    })
  })
});

Not this way:

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');
      }
    })
  })
});

My first code examle sims to be silly, but i want to have routing hierarchy. Is there any ability to do that?!


Solution

  • This kind of thing is certainly doable. There are many examples out there of nested routes. I can't check your specific Router against an a real app, but there's an example very much long this lines in the Ember Application Structure guide. See the section on Nesting. You'll find this:

    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'));
        }
      })
    })