// script for endless scrolling using jQuery // demo: http://www.jstoolbox.com/demo/endless/ var engine = { posts : [], target : null, busy : false, count : 5, render : function(obj){ var xhtml = '
'; if (obj.title) { xhtml += '

'+obj.title+'

'; } if (obj.posted_at) { xhtml += '
Posted on: '+obj.posted_at+'
'; } if (obj.comments_count) { xhtml += '
Comments: ' + obj.comments_count + '
'; } xhtml += '
' + obj.content + '
'; xhtml += '
'; return xhtml; }, init : function(posts, target){ if (!target) return; this.target = $(target); this.append(posts); var that = this; $(window).scroll(function(){ if ($(document).height() - $(window).height() <= $(window).scrollTop() + 50) { that.scrollPosition = $(window).scrollTop(); that.get(); } }); }, append : function(posts){ posts = (posts instanceof Array) ? posts : []; this.posts = this.posts.concat(posts); for (var i=0, len = posts.length; i 0) { that.append(data); } that.setBusy(false); } ); }, showLoading : function(bState){ var loading = $('#loading'); if (bState) { $(this.target).append(loading); loading.show('slow'); } else { $('#loading').hide(); } } setBusy : function(bState){ this.showLoading(this.busy = bState); } }; // usage $(document).ready(function(){ engine.init(null, $("#blog")); engine.get(); });