Skip to content

Instantly share code, notes, and snippets.

@bradenmacdonald
Created April 30, 2012 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradenmacdonald/2562364 to your computer and use it in GitHub Desktop.
Save bradenmacdonald/2562364 to your computer and use it in GitHub Desktop.
Get infinite-scroll to send X-PJAX header.
$.infinitescroll.prototype.retrieve = function (pageNum) {
var instance = this,
opts = instance.options,
path = opts.path,
box, frag, desturl, method, condition,
pageNum = pageNum || null,
getPage = (!!pageNum) ? pageNum : opts.state.currPage;
beginAjax = function infscr_ajax(opts) {
// increment the URL bit. e.g. /page/3/
opts.state.currPage++;
instance._debug('heading into ajax', path);
// if we're dealing with a table we can't use DIVs
box = $(opts.contentSelector).is('table') ? $('<tbody/>') : $('<div/>');
desturl = path.join(opts.state.currPage);
// Use PJAX to load the new part:
$.ajax({
// params
url: desturl,
dataType: opts.dataType,
//////////////////////////////////////////////////////////////////////////////////
data: {_pjax: '.pjax-target'},
beforeSend: function(xhr){ xhr.setRequestHeader('X-PJAX', 'true') },
//////////////////////////////////////////////////////////////////////////////////
complete: function infscr_ajax_callback(jqXHR, textStatus) {
condition = (typeof (jqXHR.isResolved) !== 'undefined') ? (jqXHR.isResolved()) : (textStatus === "success" || textStatus === "notmodified");
if (condition) {
box.append($(jqXHR.responseText).find(opts.itemSelector));
instance._loadcallback(box, jqXHR.responseText);
} else { instance._error('end'); }
}
});
};
// for manual triggers, if destroyed, get out of here
if (opts.state.isDestroyed) {
this._debug('Instance is destroyed');
return false;
};
// we dont want to fire the ajax multiple times
opts.state.isDuringAjax = true;
opts.loading.start.call($(opts.contentSelector)[0],opts);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment