View difference between Paste ID: 6drT8jRJ and x4nLKWvv
SHOW: | | - or go back to the newest paste.
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"
45+
    // provide public methods "api"
46
    return {
47
        open: open,
48
        getUrl: getUrl
49
    };
50-
}());
50+
}());
51
52
/*
53
	// лучше чем <a href="#" onclick="openShop()">Open shop</a>
54
	<a href="javascript:void(0);" id="openShopLink">Open shop</a>
55
56
	$('a#openShopLink').bind('click', function(e){
57
		shop.open();
58
	});
59
60
	console.log(shop.getUrl('data'));
61
*/