coupons.page.js 2.09 KB
// 不要使用es6
'use strict';
require('home/coupons.page.css');
var $ = require('yoho-jquery'),
    fastclick = require('yoho-fastclick'),
    ellipsis = require('yoho-mlellipsis');

var $employ = $('#employ'),
    $employ2 = $('#employ2'),
    statu = 0,
    page = 1,
    dic = {},
    AjaxFlag = 0; // 防止重复请求

require('../common');
ellipsis.init();

dic[statu + '_' + page] = true;
fastclick.attach(document.body);

var couponAJAX = function(statu, page) {
    if (AjaxFlag || dic[statu + '_' + page]) {
        return;
    }

    var employDom = statu === 0 ? $employ : $employ2;

    AjaxFlag = 1;
    $.ajax({
        type: 'POST',
        url: '/home/coupons',
        dataType: 'html',
        data: {
            status: statu,
            page: page
        },
        success: function(data) {
            dic[statu + '_' + page] = true; // tab切换时,防止频繁请求
            if ($(data).find('.null').html()) {
                if (page === 1) {
                    employDom.append($(data).find('.null'));
                }
                AjaxFlag = 1;
                return;
            }
            if (!$(data).find('.employ-main').html()) {
                AjaxFlag = 1;
                return;
            }
            employDom.append($(data).find('.employ-main'));
            AjaxFlag = 0;
        }
    });
};

$('.employ span').each(function(index, el) {
    $(el).on('click', function() {
        $('.employ span').removeClass('active').eq(index).addClass('active');
        if (index === 0) {
            $employ.removeClass('hide');
            $employ2.addClass('hide');
        } else {
            $employ.addClass('hide');
            $employ2.removeClass('hide');
        }
        statu = index;
        page = 1;
        AjaxFlag = 0;
        couponAJAX(statu, page);
        window.rePosFooter();
    });
});

var scrollHandler = function() {
    if ($(window).scrollTop() + $(window).height() > $('body').height() - 100) {
        page++;
        couponAJAX(statu, page);
        return;
    }
};

$(window).scroll(function() {
    window.requestAnimationFrame(scrollHandler);
});