Implementing the tickets API at the ‘tickets/add-order’ route
In Open Event Frontend, we have the ‘tickets/add-order’ route for a specific event which facilitates us to add the order based on the tickets that we create at the time of creation of event. The tickets are listed at the ‘tickets/add-order’ route where we can select the tickets required for example, ‘free’, ‘paid’, the payment type and proceed to the ticket buyer’s info page.
This is how we achieved implementing the API:
We use table to show the data to the user, the columns of which are Ticket Type, Price, Quantity, Item Total something like:
So, the workflow to achieve this is as follows:
- Query the tickets for current event.
- Have a controller to calculate the ‘Grand Total’ of the individual tickets that the user wants to buy.
- Show the tickets in our table.
Querying the tickets: Since we are using ember data, we query tickets by the following query in our model method of route.
model() {
return this.modelFor('events.view').query('tickets', {});
}
Thus, the above query shows that we get the current event by actually querying the model for route ‘events.view’ which returns the current event and then query the tickets model so that we get the tickets associated with the current event.
Since there is no UI table support for ember data, we are using a custom table for all the tables in Open Event Frontend and pass the data to it. To render the data in tables, we follow the following approach.
In our controller, we have a columns property as:
columns: [
{
propertyName : 'name',
title : 'Ticket Type'
},
{
propertyName : 'price',
title : 'Price(US$)',
disableSorting : true
},
{
propertyName : '',
title : 'Quantity',
template : 'components/ui-table/cell/cell-input-number'
},
{
propertyName : 'itemTotal',
title : 'Item Total'
}
]
The propertyName maps the property of the objects returned from the server i.e in our case, the ‘tickets’. Thus, we pass this skeleton of columns and data from the model to our component so as to render the table in view.
{{events/events-table columns=columns data=model
useNumericPagination=true
showGlobalFilter=true
showPageSize=true
}
Also, as seen from the image shown earlier in this blog post, we can see that we also need to calculate the ‘Grand Total’ of the total purchase. Thus, we have a computed property in controller to do this:
total: computed('model.@each.itemTotal', function() {
let sum = 0.0;
this.get('model').forEach(ticket => {
sum += ticket.get('itemTotal');
});
return sum;
}),
We iterate over the each ‘itemTotal’ in the model and keep on adding it so that the total purchase gets added accordingly. Lastly we show the Grand Total to the user as seen in the image shown earlier in the blog.
Thus, the user can select the tickets and proceed towards the checkout.
Resources:
Ember data official guide
Blog on ember data by Andy Crum
You must be logged in to post a comment.