gift-advance.js 1.57 KB
/**
 * 赠品/加价购
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/23
 */

var $ = require('jquery'),
    lazyLoad = require('yoho.lazyload'),
    Handlebars = require('yoho.handlebars'),
    tip = require('../plugin/tip'),
    loading = require('../plugin/loading'),
    chosePanel = require('./chose-panel');

var panelTmpl,
    $page = $('.gift-advance-page'),
    $chosePanel = $('#chose-panel');

lazyLoad($('.lazy'));

$.get('/cart/index/giftinfoTpl', function(html) {
    if (!html) {
        tip.show('网络错误');
        return;
    }
    panelTmpl = Handlebars.compile(html);
}).fail(function() {
    tip.show('网络错误');
});

function getProductInfo(skn, promotionId) {
    loading.showLoadingMask();
    $.get('/cart/index/giftinfo', {
        skn: skn,
        promotionId: promotionId
    }).then(function(res) {
        if (!res) {
            tip.show('网络错误');
            return;
        }
        if (!panelTmpl) {
            return;
        }
        if (res.code === 200) {
            $chosePanel.html(panelTmpl({
                cartInfo: res.data
            }));
            chosePanel.show();
        } else {
            tip.show(res.message || '网络错误');
        }
    }).fail(function() {
        tip.show('网络错误');
    }).always(function() {
        loading.hideLoadingMask();
    });
}

$page.on('touchend', '.chose', function() {
    var $this = $(this),
        id = $this.closest('.gift-advance-good').data('id'),
        promotionId = $this.closest('.advance-block').data('promotion-id');

    getProductInfo(id, promotionId);
});