Dynamic Segments in Open Event Frontend
In the Open Event Frontend project we have a page where we show all types of events. We have classified events into six classes like live, draft, past etc. Now each event type should have it’s own page describing it. What should we do now? Make six different routes corresponding to each class of event? Isn’t that too cumbersome. Is there any other method to do it?
Dynamic segment is the answer to the above question. Dynamic segment is that segment of the path for a route that will change based on content of the page. Dynamic segments are frequently used in Open Event Frontend.
One such use is in /admin/events. Here we have button for different categories of events. We do not make separate routes for each of them, instead we use dynamic segments. We’ll have a single route and we will change the data in the route corresponding to the tab chosen.
Lets now add dynamic segments to /admin/events. First of all we’ll add the following code snippet in router.js.
this.route(‘events’, function() { this.route(‘list’, { path: ‘/:events_status’ }); this.route(‘import’); }); |
Here : signifies the presence of dynamic segment and it is followed by an identifier. It’s the identifier by which the route matches the corresponding model to show.
Now as our route would show data depending upon the tab selected, we must change the title of page depending upon the same. For this we add the following code snippet in admin/events/list.js. Here we set the title of the page using titleToken() function and access the dynamic portion of url using params.events_status
export default Route.extend({ titleToken() { switch (this.get(‘params.events_status’)) { case ‘live’: return this.l10n.t(‘Live’); case ‘draft’: return this.l10n.t(‘Draft’); case ‘past’: return this.l10n.t(‘Past’); case ‘deleted’: return this.l10n.t(‘Deleted’); } }, model(params) { this.set(‘params’, params); return [{ // Events data }]; } }); |
We’ll now link the tabs template/admin/events to the corresponding models using Link-to the helper.Here we have linked ‘Live’, ‘Draft’, ‘Past’, ‘Deleted’ buttons to dynamic segments. Let’s understand how it works.Let’s take example of Live button.Live button is linked to admin/events/list and this list is replaced by ‘live’. So our final route becomes admin/events/live.
<div class=“ui grid stackable”> <div class=“row”> <div class=“sixteen wide column”> {{#tabbed-navigation isNonPointing=true}} {{#link-to ‘admin.events.index’ class=’item’}} {{t ‘All Events’}} {{/link-to}} {{#link-to ‘admin.events.list’ ‘live’ class=’item’}} {{t ‘All Live’}} {{/link-to}} {{#link-to ‘admin.events.list’ ‘draft’ class=’item’}} {{t ‘All Draft’}} {{/link-to}} {{#link-to ‘admin.events.list’ ‘past’ class=’item’}} {{t ‘All Past’}} {{/link-to}} {{#link-to ‘admin.events.list’ ‘deleted’ class=’item’}} {{t ‘All Deleted’}} {{/link-to}} {{#link-to ‘admin.events.import’ class=’item’}} {{t ‘Import’}} {{/link-to}} {{/tabbed-navigation}} </div> </div> <div class=“row”> {{outlet}} </div> </div> |
Additional Resources
You must be logged in to post a comment.