fav.js 2.06 KB
/**
 * 个人中心--收藏
 * @author: chenglong<chenglong.wang@yoho.cn>
 * @date: 2015/11/12
 */

var $ = require('jquery'),
    Hammer = require('yoho.hammer'),
    Swiper = require('yoho.iswiper');

var $navLi = $('#fav-tab > li'),
    $favContainer = $('.fav-content'),
    $curContainer = $favContainer.children('.fav-type').first(),
    $swiperList = $('.swiper-container'),
    swiperObj = {},
    favTabHammer,
    favContentHammer;

favTabHammer = new Hammer(document.getElementById('fav-tab'));
favTabHammer.on('tap', function(e) {
    var $cur = $(e.target).closest('li'),
        index,
        id,
        i;

    if ($cur.length === 0 || $cur.hasClass('active')) {
        return;
    }

    index = $cur.index();

    $navLi.filter('.active').removeClass('active');
    $cur.addClass('active');

    $curContainer.addClass('hide');
    $curContainer = $favContainer.children(':eq(' + index + ')').removeClass('hide');

    if (index === 1) {

        //导航
        for (i = 0; i < $swiperList.length; i++) {
            id = $swiperList.eq(i).attr('data-id');
            swiperObj[id] = new Swiper('#swiper-container-' + id, {
                slidesPerView: 'auto',
                grabCursor: true,
                slideElement: 'li',
                wrapperClass: 'swiper-wrapper-' + id,
                lazyLoading: true,
                watchSlidesVisibility: true
            });
        }

    }
});

//删除收藏的商品
favContentHammer = new Hammer(document.getElementById('fav-content'));

favContentHammer.on('tap', function (e) {
    var id = '';


    if ($(e.target).hasClass('del-fav')) {
        id = $(e.target).closest('li').attr('data-id');
        $.ajax({
            method: 'post',
            url: '/home/favoriteDel',
            data: {
                id: id
            }
        }).then(function (data) {


            if (data.code === 200) {
                $(e.target).closest('li').remove();
            }
        }).fail(function () {

            //TODO
        });
    }
});