Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. (function(){
  2.  
  3. $(document).ready(function() {
  4.  
  5. /* make a model instance and trigger data load */
  6.  
  7. window.app.model = new window.app.Shop()
  8. window.app.model.getData()
  9.  
  10. /* set up handler for dataChanged event from model */
  11.  
  12. $(window).on("dataChanged", function() {
  13.  
  14. //table
  15. var products = window.app.model.getProducts()
  16. var template = Handlebars.compile($("#productTemplate").html())
  17. var list = template({products: products})
  18. /* add to the container */
  19. $("#product").html(list)
  20.  
  21.  
  22. //show product detail
  23. $(".product-button").click(function(){
  24. var id = this.dataset.pid
  25. console.log(id)
  26. var product = window.app.model.getProduct(id)
  27. console.log(product)
  28. var template = Handlebars.compile($("#detailTemplate").html())
  29. var list = template({name : product.name, image_url : product.image_url, description : product.description, unit_cost : product.unit_cost, id : product.id})
  30. $("#productDetails").html(list)
  31.  
  32. //add the close button
  33. var el = document.getElementById('btn');
  34. console.log(el)
  35. if(el){
  36. el.addEventListener('click', function() {
  37. console.log("Close")
  38. $("#list").empty()
  39. return false
  40. })
  41. }
  42. })
  43. //cart
  44. $.get({
  45. url: "/cart",
  46. success: function(data) {
  47. var cartList = data.cart
  48. if(cartList.length === 0)
  49. {
  50. $("#cart").html("Cart is empty")
  51. }
  52. else
  53. {
  54. var totalCost = 0
  55. for(var i = 0; i<cartList.length; i++)
  56. {
  57. totalCost = totalCost + parseInt(cartList[i].quantity)*parseInt(cartList[i].cost)
  58. }
  59. $("#cart").html("Your cart has "+cartList.length+ " item(s). Total cost : $"+totalCost)
  60. }
  61. }
  62. })
  63. })
  64. })
  65.  
  66. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement