Robin
Robin

Reputation: 21884

Proper way to define a resource routes in Ember?

In the guides, I can see 2 ways to define the routes of a resource, and I was wondering what's the one I should use, and why?


Found here: http://emberjs.com/guides/routing/defining-your-routes/

App.Router.map(function() {
  this.resource('posts');
  this.resource('post', { path: '/posts/:post_id' });
});

Found here: http://emberjs.com/guides/templates/links/

App.Router.map(function() {
  this.resource("posts", function(){
    this.route("post", { path: "/:post_id" });
  });
});

I found it weird to have to define 2 resources, posts and post, when it's actually just a Post resource.

Upvotes: 4

Views: 1211

Answers (1)

Devin Crossman
Devin Crossman

Reputation: 7592

I believe when you do it like this

App.Router.map(function() {
  this.resource("posts", function(){
    this.route("post", { path: "/:post_id" });
  });
});

it renders the post template inside the {{outlet}} of the posts template..

when you do it this way

App.Router.map(function() {
  this.resource('posts');
  this.resource('post', { path: '/posts/:post_id' });
});

the posts template doesn't render when you visit /posts/:post_id

Upvotes: 5

Related Questions