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

Untitled

By: a guest on Jun 15th, 2012  |  syntax: JavaScript  |  size: 5.49 KB  |  hits: 27  |  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. var observer = (function () {
  2.  
  3.     var listeners = [],
  4.         subscribers = [];
  5.  
  6.  
  7.         return {
  8.        
  9.             // Methods that manages event-listeners
  10.             subscribe : function (listener) {
  11.                 "use strict";
  12.                
  13.                 var i;
  14.                 for (i in listener) {
  15.                     var check = $.inArray(i, subscribers);
  16.                     console.log(check);
  17.                     if (check === -1) {
  18.                         subscribers.push(i);
  19.                         listeners.push(listener[i]);
  20.                                             }
  21.                 }
  22.             },
  23.        
  24.        
  25.             publish : function (args) {
  26.                 "use strict";
  27.                 for (var i = 0; i < listeners.length; i++) {
  28.                    listeners[i]();
  29.                    
  30.                 }
  31.             }  
  32.         }
  33. }());
  34.  
  35. var product_model = (function(observer){
  36.  
  37.        
  38.         // Private area
  39.        
  40.         var addItem     = $('#add-item'),
  41.                 removeItem      = $('#rm-item'),
  42.                 products        = [],
  43.                 groups          = [],
  44.        
  45.  
  46.                
  47.                 getProducts = function () {    
  48.                         return products;       
  49.                 },
  50.                
  51.                 /**
  52.                  * Ajax call to get all availible products from db
  53.                  */
  54.                
  55.                 setProducts = function (k,v) {
  56.                        
  57.                         for (var i in v) {
  58.                                
  59.                                 var product_group = v[i];
  60.                                 $.each(product_group.product_data, function(k,v) {
  61.                                         products.push(v);
  62.                                 });    
  63.                         }
  64.                        
  65.                 },
  66.                
  67.                 setGroups = function (v) {
  68.                        
  69.                         $.each(v, function(k,v){
  70.                                 groups.push(v);
  71.                         });
  72.                        
  73.                         // Fire event when group-array is populated    
  74.                         observer.publish('getGroup');
  75.  
  76.                                                
  77.                 },
  78.                
  79.                 getGroups = function() {
  80.                         return groups;
  81.                 },
  82.                
  83.        
  84.                 /**
  85.                  * Ajax call to get all availible products from db
  86.                  */
  87.        
  88.                 get_all_products = function (url, showCart) {
  89.  
  90.                 var url = url;
  91.                
  92.                 $.ajax({
  93.                         url                     : url,
  94.                         type            : "POST",
  95.                         data            : {get_products : "1"},
  96.                         dataType        : "json",
  97.                         success         : function(data) {
  98.                                
  99.                                 //console.log(data);
  100.                                                                
  101.                                 $.each(data, function(k,v){    
  102.                                        
  103.                                                 setGroups(v);
  104.                                                 setProducts(k,v);
  105.                                 });
  106.                                
  107.                                 if (showCart == 1) {
  108.                                         showCartPopup();
  109.                                 }
  110.                                
  111.                                 tableSorter();
  112.                                 chooseNumberOfProducts();
  113.                         }
  114.                 });
  115.         },
  116.        
  117.         /**
  118.          * Ajax call to get customers previous purchases
  119.          */
  120.        
  121.         get_all_purchases = function () {
  122.  
  123.                 $.ajax({
  124.                         url                     : "../customer/store.php",
  125.                         type            : "POST",
  126.                         data            : {get_customer_summary : "1"},
  127.                         dataType        : "json",
  128.                         success         : function(data) {
  129.  
  130.                                 template = $('#customerSummaryTpl').html(),
  131.                                 html     = Mustache.to_html(template, data);
  132.  
  133.                                 $('.purchase-history').html(html);
  134.                         }
  135.                 });
  136.         },
  137.        
  138.         /**
  139.          * Ajax call to get customers previous purchases
  140.          */
  141.        
  142.         get_most_popular = function () {
  143.  
  144.                 $.ajax({
  145.                         url                     : "../customer/store.php",
  146.                         type            : "POST",
  147.                         data            : {get_most_popular : "1"},
  148.                         dataType        : "json",
  149.                         success         : function(data) {
  150.                                
  151.                                 template = $('#mostpopularTpl').html(),
  152.                                 html     = Mustache.to_html(template, data);
  153.                                 $('.most-popular-wrapper').html(html);
  154.                         }
  155.                 });
  156.         },
  157.        
  158.         /**
  159.          * Ajax call to get customers previous purchases
  160.          */
  161.        
  162.         get_customer_debt = function () {
  163.  
  164.                 $.ajax({
  165.                         url                     : "../customer/store.php",
  166.                         type            : "POST",
  167.                         data            : {get_customer_debt : "1"},
  168.                         dataType        : "json",
  169.                         success         : function(data) {
  170.  
  171.                                 template = $('#customerDebtTpl').html(),
  172.                                 html     = Mustache.to_html(template, data);
  173.                                 $('.total-purchase-overview').html(html)
  174.                                
  175.                         }
  176.                 });
  177.         },
  178.        
  179.         /**
  180.          * Functionality for add/remove products
  181.          */
  182.        
  183.         chooseNumberOfProducts = function() {
  184.                                
  185.                 addItem.live('click', function (){             
  186.  
  187.                         var inputfield = $(this).siblings('input');
  188.  
  189.                         if (!inputfield.val()) {
  190.                                 inputfield.val(1)
  191.                         } else {
  192.                                 inputfield.val(parseInt(inputfield.val()) + 1);
  193.                         }              
  194.                 });
  195.                
  196.                 removeItem.live('click', function (){          
  197.  
  198.                         var inputfield = $(this).siblings('input');
  199.  
  200.                         if (inputfield.val() > 1) {
  201.                                 inputfield.val(parseInt(inputfield.val()) - 1);
  202.                                
  203.                         } else {
  204.                                 inputfield.val('');
  205.                         }      
  206.                 });
  207.         },
  208.        
  209.         showCartPopup = function () {
  210.                
  211.                 $("#checkout-btn").click( function() {
  212.                         $("#dialog-receipt").dialog('open');
  213.                 });
  214.                 $("#dialog-receipt").dialog({
  215.                         autoOpen: false,
  216.                         resizable: true,
  217.                         width: 500,
  218.                         modal: true,
  219.                         title: 'Receipt',
  220.                         buttons: {
  221.                                 Cancel: function() {
  222.                                         $( this ).dialog( "close" );
  223.                                 },
  224.                                 "Accept": function() {
  225.                                         $( this ).dialog( "close" );
  226.                                 }
  227.                         }
  228.                 });
  229.         },
  230.        
  231.         tableSorter = function () {
  232.                
  233.                 $(".customer .tablesorter").tablesorter({
  234.                         headers: {
  235.                                 3: {
  236.                                         // disable sorting on the fourth column
  237.                                         sorter: false
  238.                                 }
  239.                         },
  240.                                 sortList : [[0,0]]
  241.                 });
  242.         }
  243.  
  244.         return {
  245.  
  246.                 get_all_products                :       get_all_products,
  247.                 get_all_purchases               :       get_all_purchases,
  248.                 get_most_popular                :       get_most_popular,
  249.                 get_customer_debt               :       get_customer_debt,
  250.                 setProducts                             :       setProducts,
  251.                 getProducts                             :       getProducts,
  252.                 setGroups                               :       setGroups,
  253.                 getGroups                               :       getGroups
  254.         };
  255.  
  256.  
  257. }(observer));
  258.  
  259.  
  260. var product_view = (function(model, observer){
  261.  
  262.        
  263.         // Private area
  264.         var model       = model,
  265.             observer    = observer,
  266.        
  267.         renderProductTpl = function () {
  268.                
  269.                 var data = { product_groups     : model.getGroups()}
  270.                                                                
  271.                                 var template = $('#productsTpl').html(),
  272.                                 html = Mustache.to_html(template, data);
  273.                
  274.                                 $('.products-list').html(html);
  275.                                
  276.                                 // listen to publish-event and execute this
  277.                                 // from observer-object when model publish event.
  278.  
  279.                                 observer.subscribe({
  280.                                     'getGroups' : function(){renderProductTpl();}
  281.                                 });
  282.         }
  283.        
  284.                
  285.         return {
  286.                
  287.                 renderProductTpl        :       renderProductTpl,
  288.                 observer                        :       observer
  289.         };
  290.  
  291.  
  292. }(product_model, observer));