Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var shop = (function(){
- var urls = {
- data: '/url/to/shop/',
- buy: '/url/to/buy/script/'
- },
- selectors = {
- shop: 'div#shop',
- shopItem: '.thing'
- };
- function open(){
- var shopNode = $(selectors.shop);
- shopNode.load(getUrl('data'), function(response, status, xhr){
- // you can write here other logic, example: alert('ssup');
- // describe events
- shopNode.delegate(selectors.shopItem, 'click', buyItem);
- });
- }
- function buyItem(e){
- var itemNode = $(this),
- itemId = itemNode.attr('data-item-id');
- $.post(getUrl('buy'), {
- itemId: itemId
- })
- .success(function(data){
- // ok, all good
- itemNode.remove();
- })
- .error(function(){
- // oops
- alert('Error');
- });
- }
- function getUrl(urlName){
- return urls[urlName];
- }
- // provide public methods "api"
- return {
- open: open,
- getUrl: getUrl
- };
- }());
Advertisement
Add Comment
Please, Sign In to add comment