Search code examples
ember.jsember-router

Emberjs: Accessing dynamic segment in nested routes/controllers


I have nested routes like this:

  this.resource('profile', { path: '/:user' }, function(){
    this.route('followers');
    this.route('following');
  });

I need an access to the value of 'user' dynamic segment in my followers/following routes/controllers. One way I have figured out is to use

  this.controllerFor('profile').get('user')

inside my followers/following routes.

What is the correct way to do this?

Thanks,


Solution

  • I think you're looking for route#modelFor.

    Example:

    App.Router.map(function() {
        this.resource('post', { path: '/post/:post_id' }, function() {
            this.resource('comments');
        });
    });
    
    App.CommentsRoute = Ember.Route.extend({
        afterModel: function() {
            this.set('post', this.modelFor('post'));
        }
    });