In the Open Event Frontend, we are having routes and it’s child routes so as to render our content on the webpage. Child routes are needed when we want to have multiple routes under a single route as a subroute. In Ember js, the child routes of a route inherit the parent content in them by default . But, in some cases, we may not want the parent template’s content to get rendered into the child route and may want the child template to be entirely different. This issue deals with the same that how can we achieve the child template entirely different from the parent one.
Above screenshots show the ‘events/tickets’ and it’s child routes. Since the ‘tickets’ is the parent and it contains the left side menu as it’s content which is common in all the child routes shown (orders, attendees, index). The content on the right of the side menu is the child’s own content. Now let’s take the case of ‘tickets/access-codes’ route which has a child route ‘tickets/access-codes/create’ where we do not want to have the parent content in it.
Following screenshots show the above thing that we want to replicate:
This is the parent template (in the issue) whose child template had to be entirely different which is shown as follows:
By default, the content of the parent template i.e ‘tickets/access-codes’ would have been rendered in create.hbs which is it’s child as shown above.
To avoid this behavior, I used the current route path which helps to customize the contents to be rendered in the child. For this I implemented a check which matches the currentRouteName with the one which we want to render (child). If it does not match, then the content of the parent will be rendered along with the child, otherwise, a separate template (only child) without the parent’s content will be rendered. Following code illustrates the same:
In this way we can customize whether to include the parent’s content in child routes or completely render different child route independent of parent’s content in it.
- Ember js guide: https://guides.emberjs.com/v1.10.0/routing/rendering-a-template/
- Ember js guide on conditionals: https://guides.emberjs.com/v2.2.0/templates/conditionals/
- oX blog on Medium: https://ox.sk/nested-routing-in-ember-js-50fa7dc82444