Authored by 郭成尧

filter-params

... ... @@ -11,7 +11,6 @@ require('./shop/coupon');
let Tab = require('plugin/tab');
let defaultOpt = require('../common/query-param');
let defaultOptCopy = {};
let $goodsContainer = $('#goods-container');
let $fsgc = $goodsContainer.children('.firstscreen-goods');
... ... @@ -22,6 +21,7 @@ let $pgc = $goodsContainer.children('.price-goods');
let $dgc = $goodsContainer.children('.discount-goods');
let $listNav = $('#list-nav'); // 筛选项列表
let $pre = $listNav.find('.active'); // 记录进入筛选前的active项,初始为选中项
let $filterBody = $('.filter-body');
let allGoodsTabClickTimes = 0;
... ... @@ -59,32 +59,22 @@ let navInfo = {
};
/**
* 判断两个对象是否相等
* @param {*} a
* @param {*} b
* 处理筛选参数
*/
function isObjectValueEqual(a, b) {
let aProps = Object.getOwnPropertyNames(a);
let bProps = Object.getOwnPropertyNames(b);
function handleChoseFilter() {
$.each($filterBody.find('.chosed'), function(index, elem) {
let choseOpt = {};
if (aProps.length != bProps.length) { // eslint-disable-line
return false;
}
for (let i = 0; i < aProps.length; i++) {
let propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
choseOpt[$(elem).parent().data('type')] = $(elem).data('id');
Object.assign(defaultOpt, choseOpt);
});
}
/**
* 获取商品列表
*/
function getGoodsList() {
handleChoseFilter();
console.log('ok');
}
... ... @@ -92,16 +82,17 @@ function getGoodsList() {
* 加载筛选数据
*/
function getFilter() {
if (allGoodsTabClickTimes === 0 || !isObjectValueEqual(defaultOptCopy, defaultOpt)) {
// 第一次点进来或者改变了筛选参数时调用
if (allGoodsTabClickTimes === 0) {
// 第一次点进来调用
defaultOptCopy = $.extend(true, {}, defaultOpt);
allGoodsTabClickTimes++;
$.ajax({
type: 'GET',
url: location.protocol + '//m.yohobuy.com/product/search/filter',
data: defaultOpt,
success: function(data) {
if (data) { // 判断需要再严禁些,确定数据正常后再 ++
allGoodsTabClickTimes++;
}
$goodsContainer.append(data);
// 初始化filter&注册filter回调
... ... @@ -114,12 +105,14 @@ function getFilter() {
$pre.siblings('.filter').removeClass('active');
}
});
$filterBody = $('.filter-body');
}
});
}
}
// 绑定 getfilter 方法,在点击全部商品时调用
// 绑定 getfilter 方法,在点击全部商品 Tab 时调用
Tab.prototype.getfilter = getFilter;
Tab.prototype.getgoodslist = getGoodsList;
... ... @@ -140,7 +133,7 @@ $listNav.on('touchend touchcancel', function(e) {
if ($this.hasClass('active')) {
filter.hideFilter();
// 点击筛选钱的active项回复active
// 点击筛选前的 active 项恢复 active
$pre.addClass('active');
$this.removeClass('active');
} else {
... ...