Guest User

Untitled

a guest
May 15th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var shop = (function(){
  2.  
  3.     var urls = {
  4.             data: '/url/to/shop/',
  5.             buy: '/url/to/buy/script/'
  6.         },
  7.         selectors = {
  8.             shop: 'div#shop',
  9.             shopItem: '.thing'
  10.         };
  11.        
  12.     function open(){
  13.         var shopNode = $(selectors.shop);
  14.        
  15.         shopNode.load(getUrl('data'), function(response, status, xhr){
  16.            
  17.             // you can write here other logic, example: alert('ssup');            
  18.            
  19.             // describe events
  20.             shopNode.delegate(selectors.shopItem, 'click', buyItem);
  21.         });
  22.     }
  23.    
  24.     function buyItem(e){
  25.         var itemNode = $(this),
  26.             itemId = itemNode.attr('data-item-id');
  27.        
  28.         $.post(getUrl('buy'), {
  29.                 itemId: itemId
  30.             })
  31.             .success(function(data){
  32.             // ok, all good
  33.                     itemNode.remove();
  34.                 })
  35.             .error(function(){
  36.             // oops
  37.                     alert('Error');
  38.                 });
  39.     }
  40.    
  41.     function getUrl(urlName){
  42.         return urls[urlName];
  43.     }
  44.  
  45.     // provide public methods "api"
  46.     return {
  47.         open: open,
  48.         getUrl: getUrl
  49.     };
  50. }());
Advertisement
Add Comment
Please, Sign In to add comment