Permalink
Browse files

Added beforeRequestCallback and afterRequestCallback. Also made the i…

…ntial bindings unbind before binding for safety.
  • Loading branch information...
1 parent 3ea878e commit b73396d34efa50d9a4876093091221efa37ef6e6 @thewalkingtoast committed Jul 12, 2012
Showing with 95 additions and 45 deletions.
  1. +33 −9 README.markdown
  2. +60 −34 js/jquery.tablePager.js
  3. +2 −2 js/jquery.tablePager.min.js
View
42 README.markdown
@@ -125,28 +125,28 @@ tablePager provides a few different options for you to override (the values show
*
*/
target: "",
-
+
/**
* The URL tablePager will request data from. Can be absolute or relative.
* -This must be provided during intialization.-
*
*/
requestURL: "",
-
+
/**
* The max number of records visible in the table at one time and
* the max to request from the server.
*
*/
pageSize: 20,
-
+
/**
* Any additional data you wish tablePager to send to the server when it
* is requesting the page change records.
*
*/
ajaxData: {},
-
+
/**
* The function to call for drawing once a response is received.
* By default, tablePager uses an interal function that simply iterates the
@@ -155,27 +155,51 @@ tablePager provides a few different options for you to override (the values show
* Provide your own function to customize the drawing behavior of tablePager.
* Must return the complete HTML as a string or a jQuery object.
*
+ * Parameters:
+ * - response - raw Ajax response
+ * - options - a JavaScript object literal with this tablePager's settings
+ * (e.g., options.pageSize, options.ajaxData, etc)
+ *
+ * Signature:
+ * callback(response, options)*
+ *
*/
requestCallback: _drawTable,
-
+
+ /**
+ * Before request callback. Allows you to be notified before a
+ * request to the server is made. If false is returned, tablePager
+ * will cancel the request.
+ *
+ */
+ beforeRequestCallback: null,
+
+ /**
+ * After request callback. Allows you to be notificed after a
+ * request has finished (post requestCallback). Useful for any
+ * additional UI setup, etc. Returns do nothing.
+ *
+ */
+ aferRequestCallback: null,
+
/**
* The jQuery selector for the 'first button' in the pager.
*
*/
firstButton: ".first-page",
-
+
/**
* The jQuery selector for the 'previous button' in the pager.
*
*/
prevButton: ".prev-page",
-
+
/**
* The jQuery selector for the 'next button' in the pager.
*
*/
nextButton: ".next-page",
-
+
/**
* The jQuery selector for the 'last button' in the pager.
*
@@ -211,4 +235,4 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
94 js/jquery.tablePager.js
@@ -1,5 +1,5 @@
/*
-* TablePager 1.01, jQuery plugin
+* TablePager 1.02, jQuery plugin
*
* Copyright(c) 2012, Adam Radabaugh
*
@@ -50,31 +50,41 @@
var options = _getOptions(pager),
postObj = $.extend({},
{"offset": options.offset, "pageSize": options.pageSize},
- options.ajaxData);
+ options.ajaxData),
+ doRequest = true;
- $.get(options.requestURL, postObj, function(response){
- if ( typeof response === "string" )
- response = $.parseJSON(response);
+ if ( typeof options.beforeRequestCallback === "function" )
+ {
+ doRequest = options.beforeRequestCallback();
+ }
- if ( response.max != null && response.max !== "" ) {
- options.resultCount = response.max;
- } else {
- console.error("No `max` was specified in result for jQuery.tablePager.");
- return;
- }
+ if ( doRequest !== false )
+ $.get(options.requestURL, postObj, function(response){
+ if ( typeof response === "string" )
+ response = $.parseJSON(response);
- var tbody = options.requestCallback(response, options);
+ if ( response.max != null && response.max !== "" ) {
+ options.resultCount = response.max;
+ } else {
+ console.error("No `max` was specified in result for jQuery.tablePager.");
+ return;
+ }
- if ( typeof options.target === "string" )
- $(options.target).html(tbody);
- else if ( typeof options.target === "object" && typeof options.target.jquery === "string" )
- options.target.html(tbody);
+ var tbody = options.requestCallback(response, options);
- _setOptions(pager, options);
- _updatePageDisplay(pager);
+ if ( typeof options.target === "string" )
+ $(options.target).html(tbody);
+ else if ( typeof options.target === "object" && typeof options.target.jquery === "string" )
+ options.target.html(tbody);
+
+ _setOptions(pager, options);
+ _updatePageDisplay(pager);
- response = options = pager = postObj = tbody = null;
- }, "JSON");
+ if ( typeof options.afterRequestCallback === "function" )
+ options.afterRequestCallback();
+
+ response = options = pager = postObj = tbody = null;
+ }, "JSON");
}
function _firstPage(pager){
@@ -152,10 +162,10 @@
function _setupPager(pager) {
var options = _getOptions(pager);
- pager.find(options.firstButton).bind("click.tablePager", function(e){e.preventDefault();_firstPage(pager);});
- pager.find(options.prevButton).bind("click.tablePager", function(e){e.preventDefault();_prevPage(pager);});
- pager.find(options.nextButton).bind("click.tablePager", function(e){e.preventDefault();_nextPage(pager);});
- pager.find(options.lastButton).bind("click.tablePager", function(e){e.preventDefault();_lastPage(pager);});
+ pager.find(options.firstButton).unbind("click.tablePager").bind("click.tablePager", function(e){e.preventDefault();_firstPage(pager);});
+ pager.find(options.prevButton).unbind("click.tablePager").bind("click.tablePager", function(e){e.preventDefault();_prevPage(pager);});
+ pager.find(options.nextButton).unbind("click.tablePager").bind("click.tablePager", function(e){e.preventDefault();_nextPage(pager);});
+ pager.find(options.lastButton).unbind("click.tablePager").bind("click.tablePager", function(e){e.preventDefault();_lastPage(pager);});
options = null;
@@ -170,28 +180,28 @@
*
*/
target: "",
-
+
/**
* The URL tablePager will request data from. Can be absolute or relative.
* -This must be provided during intialization.-
*
*/
requestURL: "",
-
+
/**
* The max number of records visible in the table at one time and
* the max to request from the server.
*
*/
pageSize: 20,
-
+
/**
* Any additional data you wish tablePager to send to the server when it
* is requesting the page change records.
*
*/
ajaxData: {},
-
+
/**
* The function to call for drawing once a response is received.
* By default, tablePager uses an interal function that simply iterates the
@@ -202,25 +212,41 @@
*
*/
requestCallback: _drawTable,
-
+
+ /**
+ * Before request callback. Allows you to be notified before a
+ * request to the server is made. If false is returned, tablePager
+ * will cancel the request.
+ *
+ */
+ beforeRequestCallback: null,
+
+ /**
+ * After request callback. Allows you to be notificed after a
+ * request has finished (post requestCallback). Useful for any
+ * additional UI setup, etc. Returns do nothing.
+ *
+ */
+ aferRequestCallback: null,
+
/**
* The jQuery selector for the 'first button' in the pager.
*
*/
firstButton: ".first-page",
-
+
/**
* The jQuery selector for the 'previous button' in the pager.
*
*/
prevButton: ".prev-page",
-
+
/**
* The jQuery selector for the 'next button' in the pager.
*
*/
nextButton: ".next-page",
-
+
/**
* The jQuery selector for the 'last button' in the pager.
*
@@ -288,12 +314,12 @@
}
if ( optionName !== "offset" && optionName !== "resultCount" &&
- optionName !== "pager" && options[optionName] )
+ optionName !== "pager" && options[optionName] )
{
options[optionName] = value;
if ( optionName === "pageSize" || optionName === "requestURL" ||
- optionName === "target" )
+ optionName === "target" )
{
options.offset = 0;
refresh = true;
View
4 js/jquery.tablePager.min.js
@@ -1,9 +1,9 @@
/*
-* TablePager 1.01, jQuery plugin
+* TablePager 1.02, jQuery plugin
*
* Copyright(c) 2012, Adam Radabaugh
*
* Simple AJAX table pagination
* Licensed under the MIT License
*/
-(function(a){var b=function(){function c(a){return a.data("jqv-table-pager")}function d(a,b){a.data("jqv-table-pager",b)}function e(a){a.removeData("jqv-table-pager")}function f(a){var b=c(a),d="1/1",e=0,f=1,g=0,h=b.offset,i=b.pageSize;f=Math.ceil(b.resultCount/i);for(;;){g+=i,e+=1;if(h<g)break}e=e||1,f=f||1,d=e+"/"+f,b.pager.find(".page-display").val(d),b=d=e=f=a=g=h=i=null}function g(b){var e=c(b),g=a.extend({},{offset:e.offset,pageSize:e.pageSize},e.ajaxData);a.get(e.requestURL,g,function(c){typeof c=="string"&&(c=a.parseJSON(c));if(c.max==null||c.max===""){console.error("No `max` was specified in result for jQuery.tablePager.");return}e.resultCount=c.max;var h=e.requestCallback(c,e);typeof e.target=="string"?a(e.target).html(h):typeof e.target=="object"&&typeof e.target.jquery=="string"&&e.target.html(h),d(b,e),f(b),c=e=b=g=h=null},"JSON")}function h(a){var b=c(a);b.offset>0&&(b.offset=0,d(a,b),g(a)),b=a=null}function i(a){var b=c(a);b.offset>0&&b.offset>=b.pageSize&&(b.offset=b.offset-b.pageSize,d(a,b),g(a)),b=a=null}function j(a){var b=c(a);b.offset<b.resultCount-b.pageSize&&(b.offset=b.offset+b.pageSize,d(a,b),g(a)),b=a=null}function k(a){var b=c(a),e=b.pageSize,f=b.resultCount,h=0;if(b.offset<b.resultCount-b.pageSize){for(;;){h+=e;if(f-h<e){b.offset=h;break}}d(a,b),g(a)}b=a=e=f=h=null}function l(b,c){var d="";return a.each(b.records,function(b,c){d+="<tr>",a.each(c,function(a,b){d=d+"<td>"+b+"</td>"}),d+="</tr>"}),d}function m(a){var b=c(a);a.find(b.firstButton).bind("click.tablePager",function(b){b.preventDefault(),h(a)}),a.find(b.prevButton).bind("click.tablePager",function(b){b.preventDefault(),i(a)}),a.find(b.nextButton).bind("click.tablePager",function(b){b.preventDefault(),j(a)}),a.find(b.lastButton).bind("click.tablePager",function(b){b.preventDefault(),k(a)}),b=null,g(a)}function n(b,c){var e=a.extend({target:"",requestURL:"",pageSize:20,ajaxData:{},requestCallback:l,firstButton:".first-page",prevButton:".prev-page",nextButton:".next-page",lastButton:".last-page"},c);return e.offset=0,e.resultCount=e.pageSize,e.pager=b,e.target==null||e.target===""?(console.error("No `target` was specified for jQuery.tablePager."),!1):e.requestURL==null||e.requestURL===""?(console.error("No `requestURL` was specified for jQuery.tablePager."),!1):(d(b,e),e=null,!0)}function o(){var a=arguments[0],b=c(a);return a.find(b.firstButton).unbind("click.tablePager"),a.find(b.prevButton).unbind("click.tablePager"),a.find(b.nextButton).unbind("click.tablePager"),a.find(b.lastButton).unbind("click.tablePager"),e(a),a}function p(){var a=arguments[0],b=c(a);return b.offset=0,d(a,b),g(a),b=null,a}function q(){var a=arguments[0],b=arguments[2],e=arguments[3],f=c(a),h=!1;if(e==null)return f.hasOwnProperty(b)?f[b]:undefined;if(b==="offset"||b==="resultCount"||b==="pager"||!f[b])return a;f[b]=e;if(b==="pageSize"||b==="requestURL"||b==="target")f.offset=0,h=!0;return d(a,f),h&&g(a),f=h=null,a}function r(a,b){b=b||{};if(a.data("jqv-table-pager")==null){if(!n(a,b))return!1;m(a)}a=null}return{init:r,option:q,refresh:p,destroy:o}}();a.fn.tablePager=function(){var c=this,d=Array.prototype.slice.call(arguments),e=d[0],f=[];return c[0]?typeof e=="string"&&e!=="init"&&b[e]?(f.push(c),a.each(d,function(a,b){f.push(b)}),b.init(c,e),b[e].apply(null,f)):(typeof e=="object"||!e?b.init(c,e):console.error("Method "+e+" does not exist in jQuery.tablePager"),d=e=f=null,c):!1}})(jQuery);
+(function(e){var t=function(){function n(e){return e.data("jqv-table-pager")}function r(e,t){e.data("jqv-table-pager",t)}function i(e){e.removeData("jqv-table-pager")}function s(e){var t=n(e),r="1/1",i=0,s=1,o=0,u=t.offset,a=t.pageSize;s=Math.ceil(t.resultCount/a);for(;;){o+=a,i+=1;if(u<o)break}i=i||1,s=s||1,r=i+"/"+s,t.pager.find(".page-display").val(r),t=r=i=s=e=o=u=a=null}function o(t){var i=n(t),o=e.extend({},{offset:i.offset,pageSize:i.pageSize},i.ajaxData),u=!0;typeof i.beforeRequestCallback=="function"&&(u=i.beforeRequestCallback()),u!==!1&&e.get(i.requestURL,o,function(n){typeof n=="string"&&(n=e.parseJSON(n));if(n.max==null||n.max===""){console.error("No `max` was specified in result for jQuery.tablePager.");return}i.resultCount=n.max;var u=i.requestCallback(n,i);typeof i.target=="string"?e(i.target).html(u):typeof i.target=="object"&&typeof i.target.jquery=="string"&&i.target.html(u),r(t,i),s(t),typeof i.afterRequestCallback=="function"&&i.afterRequestCallback(),n=i=t=o=u=null},"JSON")}function u(e){var t=n(e);t.offset>0&&(t.offset=0,r(e,t),o(e)),t=e=null}function a(e){var t=n(e);t.offset>0&&t.offset>=t.pageSize&&(t.offset=t.offset-t.pageSize,r(e,t),o(e)),t=e=null}function f(e){var t=n(e);t.offset<t.resultCount-t.pageSize&&(t.offset=t.offset+t.pageSize,r(e,t),o(e)),t=e=null}function l(e){var t=n(e),i=t.pageSize,s=t.resultCount,u=0;if(t.offset<t.resultCount-t.pageSize){for(;;){u+=i;if(s-u<i){t.offset=u;break}}r(e,t),o(e)}t=e=i=s=u=null}function c(t,n){var r="";return e.each(t.records,function(t,n){r+="<tr>",e.each(n,function(e,t){r=r+"<td>"+t+"</td>"}),r+="</tr>"}),r}function h(e){var t=n(e);e.find(t.firstButton).unbind("click.tablePager").bind("click.tablePager",function(t){t.preventDefault(),u(e)}),e.find(t.prevButton).unbind("click.tablePager").bind("click.tablePager",function(t){t.preventDefault(),a(e)}),e.find(t.nextButton).unbind("click.tablePager").bind("click.tablePager",function(t){t.preventDefault(),f(e)}),e.find(t.lastButton).unbind("click.tablePager").bind("click.tablePager",function(t){t.preventDefault(),l(e)}),t=null,o(e)}function p(t,n){var i=e.extend({target:"",requestURL:"",pageSize:20,ajaxData:{},requestCallback:c,beforeRequestCallback:null,aferRequestCallback:null,firstButton:".first-page",prevButton:".prev-page",nextButton:".next-page",lastButton:".last-page"},n);return i.offset=0,i.resultCount=i.pageSize,i.pager=t,i.target==null||i.target===""?(console.error("No `target` was specified for jQuery.tablePager."),!1):i.requestURL==null||i.requestURL===""?(console.error("No `requestURL` was specified for jQuery.tablePager."),!1):(r(t,i),i=null,!0)}function d(){var e=arguments[0],t=n(e);return e.find(t.firstButton).unbind("click.tablePager"),e.find(t.prevButton).unbind("click.tablePager"),e.find(t.nextButton).unbind("click.tablePager"),e.find(t.lastButton).unbind("click.tablePager"),i(e),e}function v(){var e=arguments[0],t=n(e);return t.offset=0,r(e,t),o(e),t=null,e}function m(){var e=arguments[0],t=arguments[2],i=arguments[3],s=n(e),u=!1;if(i==null)return s.hasOwnProperty(t)?s[t]:undefined;if(t==="offset"||t==="resultCount"||t==="pager"||!s[t])return e;s[t]=i;if(t==="pageSize"||t==="requestURL"||t==="target")s.offset=0,u=!0;return r(e,s),u&&o(e),s=u=null,e}function g(e,t){t=t||{};if(e.data("jqv-table-pager")==null){if(!p(e,t))return!1;h(e)}e=null}return{init:g,option:m,refresh:v,destroy:d}}();e.fn.tablePager=function(){var n=this,r=Array.prototype.slice.call(arguments),i=r[0],s=[];return n[0]?typeof i=="string"&&i!=="init"&&t[i]?(s.push(n),e.each(r,function(e,t){s.push(t)}),t.init(n,i),t[i].apply(null,s)):(typeof i=="object"||!i?t.init(n,i):console.error("Method "+i+" does not exist in jQuery.tablePager"),r=i=s=null,n):!1}})(jQuery);

0 comments on commit b73396d

Please sign in to comment.