installment-goods.js 2.34 KB
/**
 * ajax 请求分期专享商品|还款记录
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/08/17
 */


var $ = require('yoho-jquery'),
    tip = require('../plugin/tip'),
    loading = require('../plugin/loading');

var ListData = function(opt) {
    var self = this;

    var _scrollHandler = function() {
        var curScrollTop = $(window).scrollTop();

        // 当scroll到1/4$repayList高度后继续请求下一页数据
        if (curScrollTop > this.opt.previousScrollTop &&
            (curScrollTop + this.opt.winH >
            $(document).height() - 0.25 * this.opt.boxArea.height() - 50)) {
            this.getListData();
        }

        self.opt.previousScrollTop = curScrollTop;
    };

    self.opt = $.extend({}, {
        stopLoading: false,
        winH: $(window).height(),
        previousScrollTop: 0
    }, opt);

    $(window).scroll(function() {
        window.requestAnimationFrame(_scrollHandler.bind(self));
    });
};

ListData.prototype.getListData = function(page) {
    var self = this;

    if (page) {
        self.opt.page = page;
    }

    if (self.opt.stopLoading) {
        return;
    }
    self.opt.stopLoading = true;
    self.opt.page ++;

    loading.showLoadingMask();
    $.ajax({
        type: 'GET',
        url: self.opt.url,
        data: {
            page: this.opt.page
        },
        dataType: 'html',
        success: function(data) {
            var $this, $title;

            self.opt.stopLoading = false;

            if (data === '') {
                if (self.opt.noResult) {
                    $('.no-result').show();
                }
                self.opt.stopLoading = true;
            } else {
                self.opt.boxArea.append(data);

                if (self.opt.noResult) {
                    $('.no-result').hide();
                }
            }

            if ($('.good-detail-text').length > 0) {
                $('.good-detail-text .name').each(function() {
                    $this = $(this);
                    $title = $this.find('a');
                    $title[0].mlellipsis(2);
                });
            }

            loading.hideLoadingMask();
        },
        error: function() {
            tip.show('网络断开连接了~');
            self.opt.stopLoading = false;
            loading.hideLoadingMask();
        }
    });
};

module.exports = ListData;