index.page.js
2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* 购物车商品
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2016/12/22
*/
require('cart/index.page.css')
let testData = require('./data.json');
let $ = require('yoho-jquery');
let hbsTemplate = require('cart/index/index.hbs');
let indexObj = {
minStock: 1,
init() {
let self = this;
$.ajax({
url: '/cart/index/new/data',
type: 'POST',
success: (data) => {
$('.cart-box').html(hbsTemplate(data));
}
})
self.registerEvent();
self.loadRecommendForYou();
},
registerEvent() {
let self = this;
$('.cart-nav').on('click', 'li', function() {
$('.cart-content').eq($(this).index()).removeClass('hide').siblings('.cart-content').addClass('hide');
$(this).addClass('active').siblings().removeClass('active')
});
$('.more-box>.down-arrow').on('click', function() {
$(this).parent().toggleClass('down')
});
$('.opt>.chk').on('click', function() {
$(this).toggleClass('checked')
});
$('.check-all').on('click', function() {
$(this).find('.chk').hasClass('checked') ? $('.opt>.chk').removeClass('checked') : $('.opt>.chk').addClass('checked');
$(this).find('.chk').toggleClass('checked');
});
$('.cart-good').on('click', '.num-opt .btn', function(e) {
let maxStock = 5;
let minusEl = $(e.delegateTarget).find('.btn-minus');
let plusEl = $(e.delegateTarget).find('.btn-plus');
let goodNum = $(e.delegateTarget).find('.good-num').val();
$(this).hasClass('btn-plus') ? goodNum++ : goodNum--;
if (goodNum < self.minStock) {
console.log('不能小于0')
return;
}
if (goodNum > maxStock) {
console.log('不能大于5')
return;
}
if (goodNum === self.minStock) {
$(minusEl).addClass('disabled');
} else if($(minusEl).hasClass('disabled')) {
$(minusEl).removeClass('disabled');
}
if (goodNum === maxStock) {
$(plusEl).addClass('disabled');
} else if($(plusEl).hasClass('disabled')) {
$(plusEl).removeClass('disabled');
}
$(e.delegateTarget).find('.good-num').val(goodNum);
})
},
loadRecommendForYou() {
if ($('.recommend-for-you').length) {
require('./recommend-for-you-cart');
}
}
}
$(() => {
indexObj.init();
})