index.page.js
3 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* 购物车商品
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2016/12/22
*/
'use strict';
let cookie = require('yoho-cookie');
require('cart/index.page.css');
require('../common');
let $ = require('yoho-jquery'),
hbsTemplate = require('cart/index/index.hbs'),
lazyLoad = require('yoho-jquery-lazyload'),
loading = require('../plugin/loading');
let indexObj = {
cartData: {},
editMode: false,
posting: false,
init() {
let self = this;
$('.nav-btn').on('click', () => {
self.navClick();
});
let promise = self.refreshPage();
if (promise) {
promise.then(() => {
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) {
loading.showLoading();
return self.getCartData().then(cartData => {
return self.renderPage(cartData.data, true).then(() => {
loading.hideLoading();
});
});
}
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 = cookie.get('_cartType') || 'ordinary';
// 普通购物车空,则显示预售购物车
if (data.commonGoodsCount === 0 && data.ordinarySoldOut === 0) {
data.cartNav = false;
data.cartType = 'advance';
self.cartData = data.preSellCart;
} else if (data.presellGoodsCount === 0 && data.advanceSoldOut === 0) { // 预售购物车空,则显示普通购物车
data.cartNav = false;
data.cartType = 'ordinary';
self.cartData = data.commonCart;
} else { // 以上两个购物车中都有数据, 默认显示普通购物车
data.cartNav = true;
data.cartType = cartType || 'ordinary';
self.cartData = data.commonCart;
}
cookie.set('_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();
});