Hi,
I have a design where one route which shows a list of items and when the user clicks on any of those items, the details are shown in place of the original list.
I reference master-detail style designs in the title because that is what most people are familiar with, but this case is not master-detail. In the case of master-detail the list of items would remain shown while the details are displayed for the selected item. In my case I want the details to replace the list.
I’ve seen a lot of back-and forth on this issue about whether it’s more appropriate to nest the routes or keep them on the same level. I’ve created a jsbin for each case to show the differences in design and was hoping someone would help explain why one is better or at least add some more objective pros and cons below.
1. Example with same level Routes:
Pros: People’s main argument for this structure is a semantic opinion that routes should reflect the structure of the templates with respect to the application. Since the templates are being rendered at the same hierarchial level then the routes should also be at the same level.
Cons: The structure often ends up with things like:
this.resource('things');
this.resource('thing', {path: 'things/:id'});
To me this bad because you are having two routes with the same path which kind of goes against the DRY principal and is confusing. If this was a practice why would we ever have nested routes.
.2. Nested Routes:
Pros: Again in the semantics here, you must first visit the parent resource in order to select/visit an item from that resource, so it makes more sense for the item to be nested within.
Cons: I think people against nested routes think that overriding the Route#renderTemplate method is considered a hack since it could be avoided with the above approach.
There was another article previously that went through some troubleshooting in the area here:
I hope this article can help find best practices for this use case and hopefully we can have someone update the Ember ‘defining route’ docs:
to make this super clear.
In my opinion, the docs are misleading because they have an example with blog posts which is assumed to be referring to a true master-detail layout which, (I believe, but may be wrong) implies nested routing; however, the snippet below shows same level routes as seen here:
App.Router.map(function() {
this.resource('posts');
this.resource('post', { path: '/post/:post_id' });
});
which I think causes confusion and although we know it works, is not necessary.
Also, based on RESTful url practices, the endpoint for a post, should only add a specific identifier to the service endpoint which serves all of the posts. This is another reason for using nested resources, because you remove the ability to mess up the path naming conventions. If you add a post resource within the posts resource you are guaranteed to use that path.