![]() 12.11. My-Tickets ComponentThe my-tickets component is the user's default screen within the application; it provides the user with a list of tickets that have been assigned to him or her. This component does a large amount of dynamic table processing and tries hard to load the minimal amount of data it needs from the server. The data minimization is important because when you're viewing your ticket list, it will poll the server on a 30-second interval, looking for updated tickets. You can see the my-tickets view in Figure 12-14, which is created from myTickets.php (see Listing 12-19). Figure 12-14. The my-tickets view![]() Listing 12-19. MyTickets.php
This component has a small template but a large amount of functionality. The template contains two elements: a notice area to display a message when the table is being updated (lines 23) and the actual table that shows the user's assigned tickets. The table contains a header, which is contained in the thread tag so that the buildTable function won't remove it. It also contains a table in its body, which will be cleared when buildTable updates the table. The template is marked with an ID so that it can be pulled out during page load by the component's setup function. The tokens are in the format described in the explanation of the updateRow function, so the {$status} will be replaced with the corresponding status line from the server. This simple templating system gives a great deal of flexibility, which is shown on lines 1617, where we add a link around the ticket_id. The matching JavaScript that uses this HTML is shown in Listing 12-20. Listing 12-20. myTickets.js
MyTicket.js starts by creating three setup functions:
Lines 3846 set up the AJAX class for this component. Callbacks are set up for the listAssignedTickets method, which is used on initial page load, and for listUpdatedTickets, which is used to get updates afterward. The Ticket instance is created on line 46. The startUpdate function (lines 4861) gets the data needed to call the server's listUpdateTicket method. This method needs the last time an update was asked for, which is stored in app.since (lines 4950). It also needs a list of the current tickets being displayed, which is created by looping over the ticket table and reading the ticket_id property on each row (lines 5457). Once this data is ready, a loading indicator is shown (line 59) and the AJAX call is made (line 60). Finishing up the component is the updateTable function (lines 6391). This function takes the data returned from the server and updates the ticket table with it. The function starts by disabling the loading indicator set by the startUpdate function (line 64); it then checks to see what type of data was returned. If it is a complete set of data (lines 6568), then the buildTable function is used, and the function stops. If there is an updated set of data, we loop over the new data, inserting new rows at the top of the table or replacing current ones. Lines 7071 prepare the new row, using updateRow and its template system. Lines 7481 determine if we are doing an update or a replace; this check is done by looping over the current tickets and looking for matching ticket_ids. If the ID exists, then we are updating a current row by removing the old row and inserting the new one (lines 8788). If the ID doesn't exist in the table, we insert a new row at the top of the table. ![]() |