index.page.js
2.57 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
84
85
86
87
88
89
90
91
92
/**
* 购物车商品
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2016/12/22
*/
'use strict';
require('cart/index.page.css');
require('../common');
let $ = require('yoho-jquery'),
hbsTemplate = require('cart/index/index.hbs'),
lazyLoad = require('yoho-jquery-lazyload');
let indexObj = {
editMode: false,
posting: false,
init() {
let self = this;
$('.nav-btn').on('click', () => {
self.navClick();
});
self.refreshPage();
require('./index/recommend-for-you').init(self);
},
navClick() {
let self = this;
$('.main-wrap').toggleClass('edit');
if ($('.main-wrap').hasClass('edit')) {
self.editMode = true;
$('.chk.edit').removeClass('checked');
} else {
self.editMode = false;
}
},
getCartData() {
return $.ajax({
url: '/cart/index/new/data',
type: 'POST'
});
},
refreshPage(data) {
let self = this;
if (!data) {
return self.getCartData().then(cartData => {
return self.renderPage(cartData.data, true);
});
}
return self.renderPage(data);
},
renderPage(data, no_try) {
let self = this;
if ((!data || (!data.commonCart && !data.preSellCart)) && no_try !== true) {
self.refreshPage();
return;
}
let cartType = window.cookie('cartType') || 'ordinary';
// 普通购物车空,则显示预售购物车
if (data.commonGoodsCount === 0 && data.ordinarySoldOut === 0) {
data.cartNav = false;
data.cartType = 'advance';
} else if (data.presellGoodsCount === 0 && data.advanceSoldOut === 0) { // 预售购物车空,则显示普通购物车
data.cartNav = false;
data.cartType = 'ordinary';
} else { // 以上两个购物车中都有数据, 默认显示普通购物车
data.cartNav = true;
data.cartType = cartType || 'ordinary';
}
window.setCookie('cartType', data.cartType);
data.ordinaryCart = data.cartType === 'ordinary';
data.advanceCart = !data.ordinaryCart;
if (data.isEmptyCart) {
$('.nav-btn').hide();
} else {
$('.nav-btn').show();
}
$('.cart-box').html(hbsTemplate(data));
require('./index/cart').init(self);
require('./index/good').init(self);
lazyLoad($('img.lazy'));
return Promise.resolve();
}
};
$(() => {
indexObj.init();
});