'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i];for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } }return target; }; function Page(config) { this.defaultConfig = { //url: '', limit: 10, page: 1, moreElement: undefined, moreCallback: undefined }; this.cacheConfig = _extends({}, config || {}); this.total = 0; this.list = []; this.config = _extends({}, this.defaultConfig, config || {}); return this; } Page.prototype.init = function () { var _this = this; this.bindScroll(); return new Promise(function (resolve, reject) { _this.get().then(function (data) { _this.total = data.total; _this.list = _this.list.concat(data.list); _this.size = Math.ceil(_this.total / _this.config.limit); resolve(_this.getReturn(_this.list)); _this.bindScrollEvent(); }, function (data) { reject(data); }); }); }; Page.prototype.bindScrollEvent = function () { clearInterval(this.checkTimer); this.checkTimer = setInterval(this.checkNext.bind(this), 300); }; Page.prototype.bindScroll = function () { this.moreElement = $(this.config.moreElement); this.moreElement.html('').append('\n
姝e湪鍔犺浇
\n
鍒拌揪鏈€鍚嶞/div>\n '); this.moreElement.find('.ing').addClass('show'); this.moreElement.find('.end').removeClass('show'); }; Page.prototype.checkNext = function () { var _this2 = this; var offset = $(window).scrollTop() + $(window).height() - this.moreElement.offset().top; if (offset > 50) { this.next().then(function (data) { if (typeof _this2.config.moreCallback === 'function') { _this2.config.moreCallback(data); } }); } }; Page.prototype.getReturn = function (list) { return { list: list, full: this.list, total: this.total, size: this.size, page: this.config.page, limit: this.config.limit }; }; Page.prototype.get = function () { var _this3 = this; return new Promise(function (resolve, reject) { _this3.requesting = true; var data = _extends({}, _this3.config, { moreCallback: null, moreElement: null }); //data.url = ""; data.moreElement = ""; data.moreCallback = ""; $.ajax({ url: _this3.config.url, data: data, method: 'POST', dataType: 'json' }).then(function (data) { //console.log(JSON.stringify(data)) resolve(data); }, function (data) { reject(data); }).done(function () { _this3.requesting = false; }); }); }; Page.prototype.reset = function (config) { this.config = _extends({}, this.config, this.defaultConfig, this.cacheConfig, config || {}); this.init(); }; Page.prototype.next = function () { var _this4 = this; if (this.requesting) { return new Promise(function (resolve, reject) { setTimeout(function () { reject(); }, 0); }); } if (this.config.page >= this.size) { return new Promise(function (resolve, reject) { _this4.moreElement.find('.ing').removeClass('show'); _this4.moreElement.find('.end').addClass('show'); clearInterval(_this4.checkTimer); setTimeout(function () { reject(); }, 0); }); } this.config.page++; return new Promise(function (resolve, reject) { _this4.get().then(function (data) { //_this4.list = _this4.list.concat(data.list); resolve(_this4.getReturn(data.list)); }, function (data) { reject(data); }); }); };