From 2307dcfed751b36393e634fe1df797ebe8339ebe Mon Sep 17 00:00:00 2001 From: jasonl99 Date: Sat, 7 Jun 2014 13:44:36 -0400 Subject: [PATCH 1/2] User-overridable ajax completion functions Moved the ajaxComplete and ajaxDone functions into the record object. This allows for customization (which allows you to do jquery promises/deferred, etc). To override, create a handler to respond to the dynatable:preinit message and swap in your ajaxSuccess and ajaxDone functions. $('.dynatable').on('dynatable:preinit', function(el, dyna) { dyna.settings.ajax.ajaxSuccess = yourSuccessFunction; return dyna.settings.ajax.ajaxDone = yourDoneFunction; }) --- jquery.dynatable.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/jquery.dynatable.js b/jquery.dynatable.js index d08aba3..146e6d7 100644 --- a/jquery.dynatable.js +++ b/jquery.dynatable.js @@ -114,6 +114,23 @@ sortTypes: {}, records: null }, + ajax: { + ajaxComplete: function(response) { + this.$element.trigger('dynatable:ajax:success', response); + // Merge ajax results and meta-data into dynatables cached data + this.records.updateFromJson(response); + // update table with new records + this.dom.update(); + + if (obj.skipPushState && obj.state.initOnLoad()) { + obj.state.push(data); + } + }, + + ajaxDone: function() { + obj.processingIndicator.hide(); + } + }, writers: { _rowWriter: defaultRowWriter, _cellWriter: defaultCellWriter, @@ -207,7 +224,6 @@ processAll = function(skipPushState) { var data = {}; - this.$element.trigger('dynatable:beforeProcess', data); if (!$.isEmptyObject(this.settings.dataset.queries)) { data[this.settings.params.queries] = this.settings.dataset.queries; } @@ -232,22 +248,9 @@ type: _this.settings.dataset.ajaxMethod, dataType: _this.settings.dataset.ajaxDataType, data: data, - error: function(xhr, error) { - }, - success: function(response) { - _this.$element.trigger('dynatable:ajax:success', response); - // Merge ajax results and meta-data into dynatables cached data - _this.records.updateFromJson(response); - // update table with new records - _this.dom.update(); - - if (!skipPushState && _this.state.initOnLoad()) { - _this.state.push(data); - } - }, - complete: function() { - _this.processingIndicator.hide(); - } + error: function(xhr, error) { }, + success: _this.settings.ajax.ajaxSuccess, + complete: _this.settings.ajax.ajaxDone }; // Do not pass url to `ajax` options if blank if (this.settings.dataset.ajaxUrl) { @@ -592,6 +595,9 @@ // Create cache of original full recordset (unpaginated and unqueried) settings.dataset.originalRecords = $.extend(true, [], settings.dataset.records); }; + console.log(obj) + this.ajaxSuccess = obj.settings.ajax.ajaxSuccess + this.ajaxDone = obj.settings.ajax.ajaxDone // merge ajax response json with cached data including // meta-data and records From 34f160c7dc1095063c426d6594b5cd32e0e5d903 Mon Sep 17 00:00:00 2001 From: jasonl99 Date: Sat, 7 Jun 2014 13:46:22 -0400 Subject: [PATCH 2/2] Removed console.log --- jquery.dynatable.js | 1 - 1 file changed, 1 deletion(-) diff --git a/jquery.dynatable.js b/jquery.dynatable.js index 146e6d7..d7a3087 100644 --- a/jquery.dynatable.js +++ b/jquery.dynatable.js @@ -595,7 +595,6 @@ // Create cache of original full recordset (unpaginated and unqueried) settings.dataset.originalRecords = $.extend(true, [], settings.dataset.records); }; - console.log(obj) this.ajaxSuccess = obj.settings.ajax.ajaxSuccess this.ajaxDone = obj.settings.ajax.ajaxDone