installment.page.js 1.61 KB
var $ = require('yoho-jquery'),
    ellipsis = require('yoho-mlellipsis'),
    tip = require('../plugin/tip'),
    loading = require('../plugin/loading');

var winH = $(window).height(),
    $installmentOnly = $('.installment-only'),
    searching = false,
    page = 0;


ellipsis.init();

function search() {
    if (searching) {
        return;
    }
    searching = true;

    loading.showLoadingMask();

    $.ajax({
        type: 'GET',
        url: '/product/recom/maylike?gender=1,3',
        data: {
            page: page + 1
        },
        success: function(data) {

            if (data === '') {
                loading.hideLoadingMask();
            } else {
                searching = false;
                $('#goods-list').append(data);

                loading.hideLoadingMask();
                page++;

                $('.good-detail-text .name').each(function() {
                    var $this = $(this),
                        $title = $this.find('a');

                    $title[0].mlellipsis(2);
                });
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
            searching = false;
            loading.hideLoadingMask();
        }
    });
}

function scrollHandler() {
    if ($(window).scrollTop() + winH >= $(document).height() - 200) {
        search();
    }
}

// srcoll to load more
$(window).scroll(function() {
    if ($installmentOnly.length > 0) {
        window.requestAnimationFrame(scrollHandler);
    }
});


if ($installmentOnly.length > 0) {
    $('.installment-page').css({
        background: '#efefef'
    });

    search();
}