Giter Site home page Giter Site logo

infiniteflow's Introduction

infiniteFlow

下拉获取数据

类的实现:

function oInfiniteFlow(elId,type, getDataFunc) {
	this.el= $(elId)
	this.type=type;
	this.getData= getDataFunc;
	
	var that=this;
	this.addEventHandle= function(){
		var winHeight= $(window).height();
		$(window).bind("scroll",function(){
			if (that.isNoData || that.el.is(':hidden'))
				return;
			//that.isPending=true;//及时锁住
			var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
			var pageHeight = $(document.body).height();
			var thres= (pageHeight-winHeight-scrollTop)/winHeight;
			if (thres<0.02) {
				that.getData();
			}
		});
	}
	this.getPage= function(){
		return this.page;
	}
	this.getPageSize= function(){
		return this.pagesize;
	}

	this.loading= function(){
		if (this.isPending)
			return false; 
		this.isPending=true;
		this.el.append('<li><div class="loading"></div></li>'); 
		return true;
	}
	this.reset= function(){
		this.page=0;
		this.pagesize=15;
		this.resultBuf= new Array();
		this.isNoData= false;
		this.isPending= false;
		this.el.empty();
	}
	this.display= function(newData){
		this.resultBuf= this.resultBuf.concat(newData);
		var resultBuf= this.resultBuf;
		var page= this.page;
		var pagesize= this.pagesize;
		var type= this.type;
		var content = '';
		for (var i= page*pagesize; i<(page+1)*pagesize && resultBuf[i]; i++){
			content += genContent(resultBuf[i],type);
		}
		this.el.find(".loading").remove();
		this.el.append(content);
		this.page++;
		if (newData.length<pagesize)
			this.noData();
		//稍后再释放锁
		setTimeout(function(){that.isPending=false},500);
	}
	

	this.noData= function(){
		this.isNoData= true;
		this.el.find(".loading").remove();
		this.el.append('<li><div class="nodata"></div></li>');
		that.pending=false;
	}
	this.reset();
	this.addEventHandle();
}

用法:

//获取想去、去过XX地方的人或队伍
var urlTable={'loved':"/Travel/ajaxFindPersons",
		'visited':"/Travel/ajaxGetVisited",
		'teams':"/Travel/ajaxFindTeams"};
function commonGet(type,from,go,displayer){
	if (!displayer.loading())
		return; //如果被另外一个任务锁住,则退出
	var url=APP+urlTable[type];
	if (!url)
		return;
	$.post(url,
			{'from':from,'go':go,'page':displayer.getPage(),'page_size':displayer.getPageSize()},
			function(serverData, status) {
				var allData = eval(serverData);
				if (allData.status) {
					displayer.noData();
					return;
				}
				displayer.display(allData.data);
			}
		)
}

var infiniteFlow1= new oInfiniteFlow('#lPersons','loved',function(){
	commonGet('loved',globalFrom,globalGo,this);
});

infiniteflow's People

Contributors

firnsan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.