I have in my application a cart, it currently works for me, though the issue my colleague is having is that it is too "slow". I have been thinking of a better way to implement this to make it faster and efficient.
Currently this is how my page loads:
- Product/Ticket page loads.
- AJAX function gets products/tickets from server and displays on the page.
Each product has a buy button like this:
<button ng-click="buyTicket(id)" class="btn">Buy Ticket</button>
This is how the buyticket works:
I pass the id of the product/ticket to my function.
- AJAX function posts id to server.
- Server runs a database query to retrieve product/ticket information based on id.
- Product/ticket information is saved into "cart" table.
- AJAX function returns with data "true" as the response.
I broadcast this:
$rootScope.$broadcast('TICKET_ADDED', true);
Cart directive listens to broadcast and makes an AJAX call to server to get cart data:
$scope.$on('TICKET_ADDED', function(response) { $scope.loadCart(); })
Data returned is assigned to an array and displayed in the cart.
Each user cart is identified by a randomly generated string of length 25.
That is how my cart works for now, i would like a faster, better way to do this please.
Edit: (used a debug bar to get these statistics)
When page loads: No. of queries run: 1 Memory Usage: 12.25 MB Request Duration: 1.04s No. of AJAX requests: 3
When buy ticket function is clicked: No. of queries run: 5 Memory Usage: 12.75 MB Request Duration: 934.41 ms No. of AJAX requests: 2