Skip to content

Commit

Permalink
added onLoadItems callback,
Browse files Browse the repository at this point in the history
documentated new callback,
added Advanced Usage section to readme
  • Loading branch information
fieg committed Feb 16, 2012
1 parent 65c280c commit 70feb4b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 13 deletions.
61 changes: 60 additions & 1 deletion README.textile
Expand Up @@ -2,7 +2,7 @@ h1. Infinite Ajax Scroll

Turn your paginated pages into infinite scrolling pages with ease.

Version 0.1.2
Version 0.1.3

Requires jQuery 1.4 or newer.

Expand Down Expand Up @@ -79,6 +79,65 @@ Example:

bc. onPageChange: function(pageNum, pageUrl, scrollOffset) { console.log('Welcome on page ' + pageNum); }

h3. onLoadItems

*Default:* empty function
Event handler. Is called each time new items are loaded.

Parameters:

|*param*|*description*|
|items|array containing the item elements|

Return value:
When we return false in the callback, we prevent IAS from automatically insert the loaded items. This can be used to manually insert the items.

Example:

bc. onLoadItems: function(items) { console.log('We loaded ' + items.length + ' items'); }

h2. Advanced usage

h3. Integrating Google Analytics

You can integrate Google Analytics by using the onPageChange event. Here is an example:

bc. jQuery.ias({
container : ".listing",
item: ".post",
pagination: "#content .navigation",
next: ".next-posts a",
loader: "images/loader.gif",
onPageChange: function(pageNum, pageUrl, scrollOffset) {
// This will track a pageview every time the user
// scrolls up or down the screen to a different page.
path = jQuery('<a/>').attr('href',pageUrl)[0].pathname.replace(/^[^\/]/,'/');
_gaq.push(['_trackPageview', path]);
}
});

h3. Integrating with jQuery Masonry

bc. jQuery.ias({
container : ".listing",
item: ".post",
pagination: "#content .navigation",
next: ".next-posts a",
loader: "images/loader.gif",
onLoadItems: function(items) {
// hide new items while they are loading
var $newElems = $(items).show().css({ opacity: 0 });
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
// show elems now they're ready
$newElems.animate({ opacity: 1 });
$('.listing').masonry( 'appended', $newElems, true );
});
return true;
}
});


h2. Customizing

IAS inserts a div element with an image to generate a loader. The div has a class called *ias_loader*. Using this class you can adjust the styling of the loader.
Expand Down
22 changes: 12 additions & 10 deletions jquery.ias.js
@@ -1,6 +1,6 @@
/*!
* Infinite Ajax Scroll, a jQuery plugin
* Version v0.1.2
* Version v0.1.3
* http://webcreate.nl/
*
* Copyright (c) 2011 Jeroen Fiege
Expand Down Expand Up @@ -146,15 +146,16 @@
show_loader();

loadItems(urlNextPage, function(data, items) {
// walk through the items on the next page
// and insert them with a nice fadeIn effect
for(i=0;i<items.length;i++) {
var item = items[i];
item.hide(); // at first, hide it so we can fade it in later
// call the onLoadItems callback
result = opts.onLoadItems.call(this, items);

if (result !== false) {
$(items).hide(); // at first, hide it so we can fade it in later

// insert them after the last item with a nice fadeIn effect
curLastItem = $(opts.container).find(opts.item).last();
curLastItem.after(item);
item.fadeIn();
curLastItem.after(items);
$(items).fadeIn();
}

// update pagination
Expand Down Expand Up @@ -183,7 +184,7 @@
// walk through the items on the next page
// and add them to the items array
$(opts.container, data).find(opts.item).each(function() {
items.push($(this));
items.push(this);
});

if (onCompleteHandler) onCompleteHandler.call(this, data, items);
Expand Down Expand Up @@ -280,7 +281,8 @@
item: ".item",
pagination: "#pagination",
next: ".next",
onPageChange: function() {}
onPageChange: function() {},
onLoadItems: function() {},
};

// utility module
Expand Down
4 changes: 2 additions & 2 deletions jquery.ias.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 70feb4b

Please sign in to comment.