summaryrefslogtreecommitdiffstats
path: root/dev/jquery.jtable.core.js
diff options
context:
space:
mode:
authorhikalkan <hi_kalkan@yahoo.com>2014-03-05 21:00:35 +0200
committerhikalkan <hi_kalkan@yahoo.com>2014-03-05 21:00:35 +0200
commitbc47eca00f84edb1294526e7b468ab1064677a42 (patch)
tree5ff7f6a230a82733d0203b3092fac15152edd7af /dev/jquery.jtable.core.js
parentac038f4e00ec133d2dd9898cd0a4200aa24de103 (diff)
downloadjtable-bc47eca00f84edb1294526e7b468ab1064677a42.zip
jtable-bc47eca00f84edb1294526e7b468ab1064677a42.tar.gz
jtable-bc47eca00f84edb1294526e7b468ab1064677a42.tar.bz2
Functions as actions support
Can define functions as actions instead of URL strings. Define a function that returns a deffered or the result.
Diffstat (limited to 'dev/jquery.jtable.core.js')
-rw-r--r--dev/jquery.jtable.core.js88
1 files changed, 60 insertions, 28 deletions
diff --git a/dev/jquery.jtable.core.js b/dev/jquery.jtable.core.js
index 62a3d95..a3209ef 100644
--- a/dev/jquery.jtable.core.js
+++ b/dev/jquery.jtable.core.js
@@ -402,42 +402,68 @@
_reloadTable: function (completeCallback) {
var self = this;
- //Disable table since it's busy
- self._showBusy(self.options.messages.loadingMessage, self.options.loadingAnimationDelay);
+ var completeReload = function(data) {
+ self._hideBusy();
- //Generate URL (with query string parameters) to load records
- var loadUrl = self._createRecordLoadUrl();
+ //Show the error message if server returns error
+ if (data.Result != 'OK') {
+ self._showError(data.Message);
+ return;
+ }
+
+ //Re-generate table rows
+ self._removeAllRows('reloading');
+ self._addRecordsToTable(data.Records);
+
+ self._onRecordsLoaded(data);
+
+ //Call complete callback
+ if (completeCallback) {
+ completeCallback();
+ }
+ };
- //Load data from server
+ self._showBusy(self.options.messages.loadingMessage, self.options.loadingAnimationDelay); //Disable table since it's busy
self._onLoadingRecords();
- self._ajax({
- url: loadUrl,
- data: self._lastPostData,
- success: function (data) {
- self._hideBusy();
- //Show the error message if server returns error
- if (data.Result != 'OK') {
- self._showError(data.Message);
- return;
- }
+ //listAction may be a function, check if it is
+ if ($.isFunction(self.options.actions.listAction)) {
- //Re-generate table rows
- self._removeAllRows('reloading');
- self._addRecordsToTable(data.Records);
+ //Execute the function
+ var funcResult = self.options.actions.listAction(self._lastPostData, self._createJtParamsForLoading());
- self._onRecordsLoaded(data);
+ //Check if result is a jQuery Deferred object
+ if (self._isDeferredObject(funcResult)) {
+ funcResult.done(function(data) {
+ completeReload(data);
+ }).fail(function() {
+ self._showError(self.options.messages.serverCommunicationError);
+ }).always(function() {
+ self._hideBusy();
+ });
+ } else { //assume it's the data we're loading
+ completeReload(funcResult);
+ }
- //Call complete callback
- if (completeCallback) {
- completeCallback();
+ } else { //assume listAction as URL string.
+
+ //Generate URL (with query string parameters) to load records
+ var loadUrl = self._createRecordLoadUrl();
+
+ //Load data from server using AJAX
+ self._ajax({
+ url: loadUrl,
+ data: self._lastPostData,
+ success: function (data) {
+ completeReload(data);
+ },
+ error: function () {
+ self._hideBusy();
+ self._showError(self.options.messages.serverCommunicationError);
}
- },
- error: function () {
- self._hideBusy();
- self._showError(self.options.messages.serverCommunicationError);
- }
- });
+ });
+
+ }
},
/* Creates URL to load records.
@@ -446,6 +472,12 @@
return this.options.actions.listAction;
},
+ _createJtParamsForLoading: function() {
+ return {
+ //Empty as default, paging, sorting or other extensions can override this method to add additional params to load request
+ };
+ },
+
/* TABLE MANIPULATION METHODS *******************************************/
/* Creates a row from given record