favorite.page.js 1.45 KB
/**
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 2016/7/19
 */

var lazyload = require('yoho-jquery-lazyload');

var $root = $('.favorite-products');

require('./me');
require('../plugins/check');

function doCancel(ids) {
    $.post('/me/collection/cancel', {
        type: 'product',
        ids: ids
    }, function(data) {
        if (data.code === 200) {
            location.href = '/me/collection';
        }
    });
}

function eventBind() {
    $('.check-all', $root).check({
        type: 'checkbox',
        onChange: function(ele, checked) {
            if (checked) {
                $('.goods-info', $root).addClass('choose');
            } else {
                $('.goods-info', $root).removeClass('choose');
            }
        }
    });

    $('.goods-img', $root).click(function() {
        if ($(this).parent('.goods-info', $root).hasClass('choose')) {
            $(this).parent('.goods-info', $root).removeClass('choose');
        } else {
            $(this).parent('.goods-info', $root).addClass('choose');
        }

    });

    $('.btn.cancel', $root).click(function() {
        var id = $(this).parents('.goods-info').data('id');

        doCancel(id);
    });

    $('.favorite-cancel', $root).click(function() {
        var ids = [];

        $('.goods-info.choose', $root).each(function() {
            ids.push($(this).data('id'));
        });

        doCancel(ids.join(','));
    });
}

$(function() {
    lazyload();
});
eventBind();