shop.page.js 2.97 KB
var lazyload = require('yoho-jquery-lazyload');
var Handlebars = require('yoho-handlebars');

var tpl = '{{#each products}}' +
    '<div class="goods-info" data-skn="{{productSkn}}">' +
    '<a href="{{url}}" target="_blank">' +
    '<img class="lazy thumb" src="{{defaultImages}}" style="display: block;">' +
    '</a>' +
    '<div class="desc">' +
    '<a class="name" href="{{url}}" target="_blank">{{productName}}</a>' +
    '<p class="price">¥{{salesPrice}}</p>' +
    '</div>' +
    '</div>' +
    '{{/each}}';

var tplFn = Handlebars.compile(tpl);

require('../common/header');
require('./list/favorite');
lazyload();

$(function() {
    var shopId = $('.yoho-shop-index').data('id');
    var $order = $('.yoho-shop-index .order-page');
    var $nextPage = $('.page.page-next', $order);
    var $prePage = $('.page.page-pre', $order);
    var total = parseInt($('.total-page', $order).text(), 10);
    var page = 1;
    var orderBy;

    function refreshProducts(data) {
        $('.cur-page', $order).text(page);
        if (page === 1) {
            $($prePage).addClass('disable');
            $($nextPage).removeClass('disable');
        } else if (page === total) {
            $($nextPage).addClass('disable');
            $($prePage).removeClass('disable');
        } else {
            $($nextPage).removeClass('disable');
            $($prePage).removeClass('disable');
        }

        $('.yoho-shop-index .all-product-warp .goods-wrap').html(tplFn(data));
    }

    $($nextPage).click(function() {
        if (!$(this).hasClass('disable') && page < total) {
            $.get('/product/shop/query/all', {
                page: page++,
                shopId: shopId
            }, function(data) {
                refreshProducts(data);
            });
        }
    });

    $($prePage).click(function() {
        if (!$(this).hasClass('disable') && page > 1) {
            $.get('/product/shop/query/all', {
                page: page--,
                shopId: shopId
            }, function(data) {
                refreshProducts(data);
            });
        }
    });

    $('.order-area .order').click(function() {
        var order = $(this).data('order');
        var target = $(this).data('target');
        var orders = order.split(',');
        var newOrder = '';

        if (orderBy === orders[0]) {
            newOrder = orders[1] || '';
        } else {
            newOrder = orders[0];
        }

        $('.order-area .order').removeClass('selected');
        $(this).addClass('selected');
        $('.' + target).find('.iconfont').each(function() {
            if ($(this).hasClass(newOrder)) {
                $(this).addClass('selected');
            } else {
                $(this).removeClass('selected');
            }
        });

        orderBy = newOrder;
        page = 1;

        $.get('/product/shop/query/all', {
            page: page,
            shopId: shopId,
            order: newOrder
        }, function(data) {
            refreshProducts(data);
        });
    });
});