Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 15th, 2012  |  syntax: JavaScript  |  size: 3.39 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. var product_model = (function(){
  3.  
  4.        
  5.         // Private area
  6.        
  7.         var addItem     = $('#add-item'),
  8.                 removeItem      = $('#rm-item'),
  9.        
  10.        
  11.                 /**
  12.                  * Ajax call to get all availible products from db
  13.                  */
  14.        
  15.                 get_all_products = function (url, showCart) {
  16.  
  17.                 var url = url;
  18.                
  19.                 $.ajax({
  20.                         url                     : url,
  21.                         type            : "POST",
  22.                         data            : {get_products : "1"},
  23.                         dataType        : "json",
  24.                         success         : function(data) {
  25.  
  26.                                 var template = $('#productsTpl').html(),
  27.                                 html = Mustache.to_html(template, data);
  28.  
  29.                                 $('.products-list').html(html);
  30.                                
  31.                                 if (showCart == 1) {
  32.                                         showCartPopup();
  33.                                 }
  34.                                
  35.                                 tableSorter();
  36.                                 chooseNumberOfProducts();
  37.                         }
  38.                 });
  39.         },
  40.        
  41.         /**
  42.          * Ajax call to get customers previous purchases
  43.          */
  44.        
  45.         get_all_purchases = function () {
  46.  
  47.                 $.ajax({
  48.                         url                     : "../customer/store.php",
  49.                         type            : "POST",
  50.                         data            : {get_customer_summary : "1"},
  51.                         dataType        : "json",
  52.                         success         : function(data) {
  53.  
  54.                                 template = $('#customerSummaryTpl').html(),
  55.                                 html     = Mustache.to_html(template, data);
  56.  
  57.                                 $('.purchase-history').html(html);
  58.                         }
  59.                 });
  60.         },
  61.        
  62.         /**
  63.          * Ajax call to get customers previous purchases
  64.          */
  65.        
  66.         get_most_popular = function () {
  67.  
  68.                 $.ajax({
  69.                         url                     : "../customer/store.php",
  70.                         type            : "POST",
  71.                         data            : {get_most_popular : "1"},
  72.                         dataType        : "json",
  73.                         success         : function(data) {
  74.                                
  75.                                 template = $('#mostpopularTpl').html(),
  76.                                 html     = Mustache.to_html(template, data);
  77.                                 $('.most-popular-wrapper').html(html);
  78.                         }
  79.                 });
  80.         },
  81.        
  82.         /**
  83.          * Ajax call to get customers previous purchases
  84.          */
  85.        
  86.         get_customer_debt = function () {
  87.  
  88.                 $.ajax({
  89.                         url                     : "../customer/store.php",
  90.                         type            : "POST",
  91.                         data            : {get_customer_debt : "1"},
  92.                         dataType        : "json",
  93.                         success         : function(data) {
  94.  
  95.                                 template = $('#customerDebtTpl').html(),
  96.                                 html     = Mustache.to_html(template, data);
  97.                                 $('.total-purchase-overview').html(html)
  98.                                
  99.                         }
  100.                 });
  101.         },
  102.        
  103.         /**
  104.          * Functionality for add/remove products
  105.          */
  106.        
  107.         chooseNumberOfProducts = function() {
  108.                                
  109.                 addItem.live('click', function (){             
  110.  
  111.                         var inputfield = $(this).siblings('input');
  112.  
  113.                         if (!inputfield.val()) {
  114.                                 inputfield.val(1)
  115.                         } else {
  116.                                 inputfield.val(parseInt(inputfield.val()) + 1);
  117.                         }              
  118.                 });
  119.                
  120.                 removeItem.live('click', function (){          
  121.  
  122.                         var inputfield = $(this).siblings('input');
  123.  
  124.                         if (inputfield.val() > 1) {
  125.                                 inputfield.val(parseInt(inputfield.val()) - 1);
  126.                                
  127.                         } else {
  128.                                 inputfield.val('');
  129.                         }      
  130.                 });
  131.         },
  132.        
  133.         showCartPopup = function () {
  134.                
  135.                 $("#checkout-btn").click( function() {
  136.                         $("#dialog-receipt").dialog('open');
  137.                 });
  138.                 $("#dialog-receipt").dialog({
  139.                         autoOpen: false,
  140.                         resizable: true,
  141.                         width: 500,
  142.                         modal: true,
  143.                         title: 'Receipt',
  144.                         buttons: {
  145.                                 Cancel: function() {
  146.                                         $( this ).dialog( "close" );
  147.                                 },
  148.                                 "Accept": function() {
  149.                                         $( this ).dialog( "close" );
  150.                                 }
  151.                         }
  152.                 });
  153.         },
  154.        
  155.         tableSorter = function () {
  156.                
  157.                 $(".customer .tablesorter").tablesorter({
  158.                         headers: {
  159.                                 3: {
  160.                                         // disable sorting on the fourth column
  161.                                         sorter: false
  162.                                 }
  163.                         },
  164.                                 sortList : [[0,0]]
  165.                 });
  166.         }
  167.  
  168.         return {
  169.  
  170.                 get_all_products                :       get_all_products,
  171.                 get_all_purchases               :       get_all_purchases,
  172.                 get_most_popular                :       get_most_popular,
  173.                 get_customer_debt               :       get_customer_debt,
  174.                 chooseNumberOfProducts  :       chooseNumberOfProducts
  175.  
  176.         };
  177.  
  178.  
  179. }());