andrew4582

loadonscroll.js

Aug 13th, 2011
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.26 KB | None | 0 0
  1. /*
  2.  
  3.     Updated: Saturday August 13,2011 2:55 PM
  4.     By: Andrew Hodgson
  5.  
  6.     loadonscroll widget
  7.    
  8.     <div id="mainarea">
  9.         <div id="contentarea">
  10.         </div>
  11.     </div>
  12.  
  13.     $(function () {
  14.         $("#mainarea").loadonscroll({
  15.             targetId: "#contentarea",
  16.             url: "/GetData"
  17.         });
  18.     });
  19.  
  20. */
  21. $.widget("ui.loadonscroll", {
  22.     options: {
  23.         targetId: null,     /* target element to append content to when scroll event is handled*/
  24.         getparameters: null, /* optional function to get parameters to be passed to the $.ajax.data option, function is passed ref to widget*/
  25.         getdata: null,      /* optional function to return the scroll data manually,if this option is set then the widget will INGORE any ajax calls*/
  26.         success: null,      /* $.ajax.success() event handler*/
  27.         error: null,        /* $.ajax.error() event handler*/
  28.         type: "GET",       /* $.ajax.type option */
  29.         url: null,          /* $.ajax.url option */
  30.         contentType: "application/x-www-form-urlencoded", /* $.ajax.contentType option default is form, i.e. json: "application/json; charset=utf-8"*/
  31.         dataType: "json",   /* $.ajax.dataType option */
  32.         cache: true        /* $.ajax.cache option */
  33.     },
  34.     _create: function () {
  35.  
  36.         var options = this.options;
  37.         var selfelement = this.element;
  38.         var target = $(options.targetId);
  39.  
  40.         selfelement.scroll(function () {
  41.  
  42.             var self = $(this);
  43.             var scrollTop = self.scrollTop();
  44.             var sheight = self.height();
  45.             var theight = target.height();
  46.             var contentLoadTriggered = self.data("contentLoadTriggered") || false;
  47.             var noMoreItems = self.data("noMoreItems") || false;
  48.             var successfunc = options.success;
  49.             var errorfunc = options.error;
  50.             var getparametersfunc = options.getparameters;
  51.             var getdatafunc = options.getdata;
  52.  
  53.             if (scrollTop >= (theight - sheight) && contentLoadTriggered === false && noMoreItems === false) {
  54.                 self.data("contentLoadTriggered", true);
  55.  
  56.                 /*if options.getdata then call local function and process target with results */
  57.                 if (getdatafunc != null && $.isFunction(getdatafunc)) {
  58.  
  59.                     var resp = getdatafunc(self, options);
  60.  
  61.                     self.data("contentLoadTriggered", false);
  62.  
  63.                     if (resp == null || resp.d === null || resp.d === undefined || resp.d === "") {
  64.                         console.debug("getdatafunc -> noMoreItems->true");
  65.                         self.data("noMoreItems", true);
  66.                     }
  67.                     else {
  68.                         if (successfunc != null && successfunc != undefined) {
  69.                             if ($.isFunction(successfunc)) {
  70.                                 successfunc(target, resp, status, xhr);
  71.                                 return;
  72.                             }
  73.                         }
  74.                         console.debug("getdatafunc-> target->append: resp.d");
  75.                         target.append(resp.d);
  76.                     }
  77.                 }
  78.                 else {
  79.                     /*make ajax call to retrieve content*/
  80.                     $.ajax({
  81.                         type: options.type || "POST",
  82.                         url: options.url,
  83.                         data: getparametersfunc === null ? "{}" : getparametersfunc(self),
  84.                         contentType: options.contentType || "application/x-www-form-urlencoded",
  85.                         dataType: options.dataType || "json",
  86.                         async: true,
  87.                         cache: options.cache || false,
  88.                         success: function (resp, status, xhr) {
  89.  
  90.                             self.data("contentLoadTriggered", false);
  91.  
  92.                             if (resp == null || resp.d === null || resp.d === undefined || resp.d === "") {
  93.                                 console.debug("noMoreItems->true");
  94.                                 self.data("noMoreItems", true);
  95.                             }
  96.                             else {
  97.                                 if (successfunc != null && successfunc != undefined) {
  98.                                     if ($.isFunction(successfunc)) {
  99.                                         successfunc(target, resp, status, xhr);
  100.                                         return;
  101.                                     }
  102.                                 }
  103.                                 console.debug("target->append: resp.d");
  104.                                 target.append(resp.d);
  105.                             }
  106.                         },
  107.                         error: function (e, xhr) {
  108.  
  109.                             self.data("contentLoadTriggered", false);
  110.  
  111.                             if (errorfunc != null && errorfunc != undefined) {
  112.                                 if ($.isFunction(errorfunc)) {
  113.                                     errorfunc(e, xhr);
  114.                                 }
  115.                             }
  116.                         }
  117.                     });
  118.                 }
  119.             }
  120.         }); /* end scroll*/
  121.     } /*end _create*/
  122. });  /*end loadonscroll widget*/
Advertisement
Add Comment
Please, Sign In to add comment