Pushok

Bucket test script

Nov 3rd, 2015
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var bucket = new Map();
  2.  
  3. function Book(name1, price1){
  4.     this.name = name1;
  5.     this.price = price1;
  6.     this.count = 1;
  7.    
  8.     this.getName = function(){return this.name;};
  9.     this.getPrice = function(){return this.price;};
  10.     this.inc = function(){return ++this.count;};
  11.     this.getCount = function(){return this.count;};
  12. }
  13.  
  14.  
  15. function updateBucketForm(){
  16.     var form = $("#bucket");
  17.     form.attr("style", "list-style-type: none;");
  18.  
  19.     if(bucket.length == 0){
  20.         form.empty();
  21.         form.append("<h1>Ваша корзина пуста</h1>");
  22.     }
  23.     else{
  24.         form.empty();
  25.         var sum = 0;
  26.        
  27.         bucket.forEach(function(book){
  28.             form.append("<li><h3>"+book.getName()+" (" + book.getCount() + " шт.)</h3></li>");
  29.             sum += +book.getPrice()*book.getCount();
  30.         })
  31.        
  32.         form.append("<li><h3>Общая сумма товаров: " + sum + " руб.<h3></li>");
  33.         form.append("<button id=\"clean-bucket\">Очистить корзину</button>");
  34.         $("#clean-bucket").click(function(){
  35.             bucket = new Map();
  36.             updateBucketForm();
  37.         })
  38.     }
  39. }
  40.  
  41. $(document).ready( function(){
  42.     updateBucketForm();
  43.    
  44.     $('.thumbnail').each(function(index) {
  45.         $(this).find("button").click(function(){
  46.             var book_name = $(this).parent().attr("data-name");
  47.             var book_price = $(this).parent().attr("data-price");
  48.            
  49.             var value = bucket.get(book_name);
  50.             if(typeof value != "undefined"){
  51.                 value.inc();
  52.                 bucket.set(book_name, value);
  53.             }
  54.             else{
  55.                 bucket.set(book_name, new Book(book_name, book_price));
  56.             }
  57.            
  58.             updateBucketForm();
  59.         })
  60.     })
  61. })
Advertisement
Add Comment
Please, Sign In to add comment