latest-walk.js 1.81 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 dataLazyLoad = require('../../plugins/lazy-load')(document);

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

function fetchLatestWalk() {
    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'));
                }
            }
        }
    });
}

window.fetchLatestWalk = fetchLatestWalk;

// 数据懒加载
dataLazyLoad.init({cls: 'latest-walk-datalazyload', threshold: 700});