From 5580535c048cefc691fbb184ee0f85fa157ada53 Mon Sep 17 00:00:00 2001 From: Amichai Schreiber Date: Thu, 16 Jul 2015 12:18:21 +0300 Subject: [PATCH 1/2] enable using $.ajax instead of load() using a callback --- README.md | 4 +++- jquery.jscroll.js | 12 ++++++++++-- jquery.jscroll.min.js | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b4418b3..fdff4ae 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ $('.jscroll').jscroll({ * `contentSelector ('')` - A convenience selector for loading only part of the content in the response for the next set of content. This selector will be ignored if left blank and will apply the entire response to the DOM. * `pagingSelector ('')` - Optionally define a selector for your paging controls so that they will be hidden, instead of just hiding the next page link. * `callback (false)` - Optionally define a callback function to be called after a set of content has been loaded. +* `loadFunc (null)` - Optionally define an alternate loading function (the default being jQuery.load). Use this to load data +e.g. using POST instead of GET, or AJAX with data that is not known at the time of the last page load. The callback expects the target DOM element to be `this` (as in load()), so please use: `callback.call(target, ...)`, and not just `callback(...)`. For more information on the `contentSelector` option and how it loads a response fragment, see the [jQuery documentation for the .load() method](http://api.jquery.com/load/). @@ -56,4 +58,4 @@ In lieu of a formal styleguide, take care to maintain the existing coding style. ## LICENSES: * MIT: http://www.opensource.org/licenses/mit-license.php -* GPL-2.0: http://www.gnu.org/licenses/gpl-2.0.html \ No newline at end of file +* GPL-2.0: http://www.gnu.org/licenses/gpl-2.0.html diff --git a/jquery.jscroll.js b/jquery.jscroll.js index b2328ca..307f692 100644 --- a/jquery.jscroll.js +++ b/jquery.jscroll.js @@ -29,6 +29,7 @@ nextSelector: 'a:last', contentSelector: '', pagingSelector: '', + loadFunc: null, // function (jscrollData, domTarget, on_complete) { domTarget.html("arbitrary content"); on_complete.call(domTarget, success ? null : "error"); } callback: false } }; @@ -142,6 +143,10 @@ } }, + _defaultLoadFunc = function (data, target, callback) { + target.load(data.nextHref, function (r, status) { callback(status); }); + }, + // Load the next set of content, if available _load = function() { var $inner = $e.find('div.jscroll-inner').first(), @@ -152,8 +157,11 @@ .children('.jscroll-added').last() .html('
' + _options.loadingHtml + '
'); + var loadFunc = _options.loadFunc || _defaultLoadFunc; return $e.animate({scrollTop: $inner.outerHeight()}, 0, function() { - $inner.find('div.jscroll-added').last().load(data.nextHref, function(r, status) { + loadFunc(data, + $inner.find('div.jscroll-added').last(), + function(status) { if (status === 'error') { return _destroy(); } @@ -216,4 +224,4 @@ }); }; -})(jQuery); \ No newline at end of file +})(jQuery); diff --git a/jquery.jscroll.min.js b/jquery.jscroll.min.js index fe050c0..380cc2a 100644 --- a/jquery.jscroll.min.js +++ b/jquery.jscroll.min.js @@ -14,4 +14,4 @@ * @requires jQuery v1.4.3+ * @preserve */ -!function(a){"use strict";a.jscroll={defaults:{debug:!1,autoTrigger:!0,autoTriggerUntil:!1,loadingHtml:"Loading...",padding:0,nextSelector:"a:last",contentSelector:"",pagingSelector:"",callback:!1}};var b=function(b,c){var d=b.data("jscroll"),e="function"==typeof c?{callback:c}:c,f=a.extend({},a.jscroll.defaults,e,d||{}),g="visible"===b.css("overflow-y"),h=b.find(f.nextSelector).first(),i=a(window),j=a("body"),k=g?i:b,l=a.trim(h.attr("href")+" "+f.contentSelector),m=function(){var b=a(f.loadingHtml).filter("img").attr("src");if(b){var c=new Image;c.src=b}},n=function(){b.find(".jscroll-inner").length||b.contents().wrapAll('
')},o=function(a){var b;f.pagingSelector?a.closest(f.pagingSelector).hide():(b=a.parent().not(".jscroll-inner,.jscroll-added").addClass("jscroll-next-parent").hide(),b.length||a.wrap('
').parent().hide())},p=function(){return k.unbind(".jscroll").removeData("jscroll").find(".jscroll-inner").children().unwrap().filter(".jscroll-added").children().unwrap()},q=function(){n();var a=b.find("div.jscroll-inner").first(),c=b.data("jscroll"),d=parseInt(b.css("borderTopWidth"),10),e=isNaN(d)?0:d,h=parseInt(b.css("paddingTop"),10)+e,i=g?k.scrollTop():b.offset().top,j=a.length?a.offset().top:0,l=Math.ceil(i-j+k.height()+h);return!c.waiting&&l+f.padding>=a.outerHeight()?(u("info","jScroll:",a.outerHeight()-l,"from bottom. Loading next request..."),t()):void 0},r=function(a){return a=a||b.data("jscroll"),a&&a.nextHref?(s(),!0):(u("warn","jScroll: nextSelector not found - destroying"),p(),!1)},s=function(){var a=b.find(f.nextSelector).first();a.length&&(f.autoTrigger&&(f.autoTriggerUntil===!1||f.autoTriggerUntil>0)?(o(a),j.height()<=i.height()&&q(),k.unbind(".jscroll").bind("scroll.jscroll",function(){return q()}),f.autoTriggerUntil>0&&f.autoTriggerUntil--):(k.unbind(".jscroll"),a.bind("click.jscroll",function(){return o(a),t(),!1})))},t=function(){var c=b.find("div.jscroll-inner").first(),d=b.data("jscroll");return d.waiting=!0,c.append('
').children(".jscroll-added").last().html('
'+f.loadingHtml+"
"),b.animate({scrollTop:c.outerHeight()},0,function(){c.find("div.jscroll-added").last().load(d.nextHref,function(c,e){if("error"===e)return p();var g=a(this).find(f.nextSelector).first();d.waiting=!1,d.nextHref=g.attr("href")?a.trim(g.attr("href")+" "+f.contentSelector):!1,a(".jscroll-next-parent",b).remove(),r(),f.callback&&f.callback.call(this),u("dir",d)})})},u=function(a){if(f.debug&&"object"==typeof console&&("object"==typeof a||"function"==typeof console[a]))if("object"==typeof a){var b=[];for(var c in a)"function"==typeof console[c]?(b=a[c].length?a[c]:[a[c]],console[c].apply(console,b)):console.log.apply(console,b)}else console[a].apply(console,Array.prototype.slice.call(arguments,1))};return b.data("jscroll",a.extend({},d,{initialized:!0,waiting:!1,nextHref:l})),n(),m(),s(),a.extend(b.jscroll,{destroy:p}),b};a.fn.jscroll=function(c){return this.each(function(){var d,e=a(this),f=e.data("jscroll");f&&f.initialized||(d=new b(e,c))})}}(jQuery); \ No newline at end of file +!function(a){"use strict";a.jscroll={defaults:{debug:!1,autoTrigger:!0,autoTriggerUntil:!1,loadingHtml:"Loading...",padding:0,nextSelector:"a:last",contentSelector:"",pagingSelector:"",loadFunc:null,callback:!1}};var b=function(b,c){var d=b.data("jscroll"),e="function"==typeof c?{callback:c}:c,f=a.extend({},a.jscroll.defaults,e,d||{}),g="visible"===b.css("overflow-y"),h=b.find(f.nextSelector).first(),i=a(window),j=a("body"),k=g?i:b,l=a.trim(h.attr("href")+" "+f.contentSelector),m=function(){var b=a(f.loadingHtml).filter("img").attr("src");if(b){var c=new Image;c.src=b}},n=function(){b.find(".jscroll-inner").length||b.contents().wrapAll('
')},o=function(a){var b;f.pagingSelector?a.closest(f.pagingSelector).hide():(b=a.parent().not(".jscroll-inner,.jscroll-added").addClass("jscroll-next-parent").hide(),b.length||a.wrap('
').parent().hide())},p=function(){return k.unbind(".jscroll").removeData("jscroll").find(".jscroll-inner").children().unwrap().filter(".jscroll-added").children().unwrap()},q=function(){n();var a=b.find("div.jscroll-inner").first(),c=b.data("jscroll"),d=parseInt(b.css("borderTopWidth"),10),e=isNaN(d)?0:d,h=parseInt(b.css("paddingTop"),10)+e,i=g?k.scrollTop():b.offset().top,j=a.length?a.offset().top:0,l=Math.ceil(i-j+k.height()+h);return!c.waiting&&l+f.padding>=a.outerHeight()?(v("info","jScroll:",a.outerHeight()-l,"from bottom. Loading next request..."),u()):void 0},r=function(a){return a=a||b.data("jscroll"),a&&a.nextHref?(s(),!0):(v("warn","jScroll: nextSelector not found - destroying"),p(),!1)},s=function(){var a=b.find(f.nextSelector).first();a.length&&(f.autoTrigger&&(f.autoTriggerUntil===!1||f.autoTriggerUntil>0)?(o(a),j.height()<=i.height()&&q(),k.unbind(".jscroll").bind("scroll.jscroll",function(){return q()}),f.autoTriggerUntil>0&&f.autoTriggerUntil--):(k.unbind(".jscroll"),a.bind("click.jscroll",function(){return o(a),u(),!1})))},t=function(a,b,c){b.load(a.nextHref,function(a,b){c(b)})},u=function(){var c=b.find("div.jscroll-inner").first(),d=b.data("jscroll");d.waiting=!0,c.append('
').children(".jscroll-added").last().html('
'+f.loadingHtml+"
");var e=f.loadFunc||t;return b.animate({scrollTop:c.outerHeight()},0,function(){e(d,c.find("div.jscroll-added").last(),function(c){if("error"===c)return p();var e=a(this).find(f.nextSelector).first();d.waiting=!1,d.nextHref=e.attr("href")?a.trim(e.attr("href")+" "+f.contentSelector):!1,a(".jscroll-next-parent",b).remove(),r(),f.callback&&f.callback.call(this),v("dir",d)})})},v=function(a){if(f.debug&&"object"==typeof console&&("object"==typeof a||"function"==typeof console[a]))if("object"==typeof a){var b=[];for(var c in a)"function"==typeof console[c]?(b=a[c].length?a[c]:[a[c]],console[c].apply(console,b)):console.log.apply(console,b)}else console[a].apply(console,Array.prototype.slice.call(arguments,1))};return b.data("jscroll",a.extend({},d,{initialized:!0,waiting:!1,nextHref:l})),n(),m(),s(),a.extend(b.jscroll,{destroy:p}),b};a.fn.jscroll=function(c){return this.each(function(){var d,e=a(this),f=e.data("jscroll");f&&f.initialized||(d=new b(e,c))})}}(jQuery); \ No newline at end of file From 476f27ce766d6fb3ef98ced58dad7709fa4f1694 Mon Sep 17 00:00:00 2001 From: Amichai Schreiber Date: Thu, 10 Mar 2016 16:18:48 +0200 Subject: [PATCH 2/2] fix Trigger API's default behavior, per jevanlingen's comment --- jquery.jscroll.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.jscroll.js b/jquery.jscroll.js index 307f692..cd64a85 100644 --- a/jquery.jscroll.js +++ b/jquery.jscroll.js @@ -144,7 +144,7 @@ }, _defaultLoadFunc = function (data, target, callback) { - target.load(data.nextHref, function (r, status) { callback(status); }); + target.load(data.nextHref, function (r, status) { $.proxy(callback, target)(status); }); }, // Load the next set of content, if available