latest-walk.js 1.61 KB
/**
 * 最近浏览取接口渲染模板
 * @author: xuqi<qi.xu@yoho.cn>
 8 @date: 2016/1/20
 */

var $ = require('yoho-jquery');
var Handlebars = require('yoho-handlebars');
var lazyLoad = require('yoho-jquery-lazyload');

var $latestWalkCount = $('#latest-walk-count');

(function() {
    var tpl;

    if ($latestWalkCount.length === 0) {
        return;
    }

    tpl = Handlebars.compile($('#latest-walk-tpl').html());

    $.ajax({
        url: 'http://www.yohobuy.com/common/recentReview',
        dataType: 'jsonp',
        data: {
            limit: $latestWalkCount.val()
        },
        success: function(data) {
            var latestWalk = [],
                res, i, cur;

            if (data.code === 200) {
                res = data.data;

                // 若无最近浏览,请隐藏容器
                if (res.length === 0) {
                    $('.latest-walk').addClass('hide');
                    return;
                }
                for (i = 0; i < res.length; i++) {
                    cur = res[i];

                    latestWalk.push({
                        href: cur.url,
                        img: cur.pic_url,
                        name: cur.product_name,
                        salePrice: cur.price,
                        marketPrice: cur.market_price
                    });
                }

                if (latestWalk.length > 0) {
                    $('#latest-walk-goods').html(tpl({
                        latestWalk: latestWalk
                    }));

                    lazyLoad($('#latest-walk-goods .lazy'));
                }
            }
        }
    });
}());