简体   繁体   中英

Nested routes in Ember.js

I have an ember app which requires the following views:


  • One to view all reviews that you've posted - /reviews
  • One to view a user's profile - /users/:id
  • One to view all reviews that another user has posted - /users/:id/reviews

I am struggling with the third one.

Here is my router.js

this.route('reviews', function() {
  this.route('show', { path: "/:id" });
});

this.route('users', function(){
  this.route('show', {path: '/:id'}, function () {
    this.route("reviews", function(){});
  });
});

My template for this route is in templates/users/show/reviews/index.hbs

And my route is in routes/users/show/reviews/index.js

But when I visit localhost:4200/users/1/reviews , it shows the user profile path ( users/:id - the second bullet point). Like exactly the same

How do I get this route to work with the correct template and route.js file?

Your route should be like:

this.resource('reviews', function() {
  this.route('show', { path: '/:id'})
})

The previous means you will have available the following routes:

/reviews <- default route index for resource reviews

/reviews/:id <- this is the route show

And should have the following files:

Route -> app/routes/reviews/show.js , app/routes/reviews/index.js

Controller -> app/controllers/reviews/show.js , app/controllers/reviews/index.js

Template -> app/templates/reviews/show.hbs , app/templates/reviews/index.hbs

Note that if if you do not define any of previous files ember will generate one by default.

For users should follow the same logic that previous definition.

this.resource('users', function() {
  this.resource('user', { path: '/:id' }, function () {
    this.route("reviews");
   });
});

Translate the previous definition for Users you will have the follow route.

/users <- the default index for resource users

/users/:id <- the default index for resource user

/users/:id/reviews <- the route for reviews inside users

Hope help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM