Updated Branches: refs/heads/master 5e7c2c1ac -> aa886f40f
WICKET-3367 Rewrite all JavaScript inline event handlers to be proper attached event handlers Pass all available arguments to the different ajax call listener methods (before, after, success, failure, complete) and document them. Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/aa886f40 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/aa886f40 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/aa886f40 Branch: refs/heads/master Commit: aa886f40fe6a3c9ae4b248178024ca565ad7d052 Parents: 0c506d0 Author: martin-g <[email protected]> Authored: Wed Jan 4 10:56:26 2012 +0200 Committer: martin-g <[email protected]> Committed: Wed Jan 4 10:56:26 2012 +0200 ---------------------------------------------------------------------- .../wicket/ajax/AbstractDefaultAjaxBehavior.java | 2 +- .../wicket/ajax/attributes/IAjaxCallListener.java | 36 +++++ .../ajax/attributes/JavaScriptPrecondition.java | 9 +- .../wicket/ajax/res/js/wicket-ajax-jquery.js | 18 ++- wicket-core/src/test/js/ajax.js | 104 +++++++++++++++ 5 files changed, 159 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/aa886f40/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java index ecc8a50..c778b14 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java @@ -274,7 +274,7 @@ public abstract class AbstractDefaultAjaxBehavior extends AbstractAjaxBehavior CharSequence completeHandler = ajaxCallListener.getCompleteHandler(component); if (Strings.isEmpty(completeHandler) == false) { - attributesJson.append("ch", completeHandler); + attributesJson.append("coh", completeHandler); } } http://git-wip-us.apache.org/repos/asf/wicket/blob/aa886f40/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/IAjaxCallListener.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/IAjaxCallListener.java b/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/IAjaxCallListener.java index f1f6245..06c086e 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/IAjaxCallListener.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/IAjaxCallListener.java @@ -33,6 +33,15 @@ import org.apache.wicket.IClusterable; public interface IAjaxCallListener extends IClusterable { /** + * The JavaScript that will be executed after successful return of the Ajax call. + * The script will be executed in a function that receives the following parameters: + * <ol> + * <li>data - the Ajax response. Its type depends on {@link AjaxRequestAttributes#dataType}</li> + * <li>textStatus - the status as text</li> + * <li>jqXHR - the jQuery XMLHttpRequest object</li> + * <li>attrs - the AjaxRequestAttributes as JSON</li> + * </ol> + * * @param component * the Component with the Ajax behavior * @return the JavaScript that will be executed after a successful @@ -41,6 +50,12 @@ public interface IAjaxCallListener extends IClusterable CharSequence getSuccessHandler(Component component); /** + * The JavaScript that will be executed after unsuccessful return of the Ajax call. + * The script will be executed in a function that receives the following parameters: + * <ol> + * <li>attrs - the AjaxRequestAttributes as JSON</li> + * </ol> + * * @param component * the Component with the Ajax behavior * @return the JavaScript that will be executed after a unsuccessful @@ -49,6 +64,14 @@ public interface IAjaxCallListener extends IClusterable CharSequence getFailureHandler(Component component); /** + * The JavaScript that will be executed before the Ajax call. + * The script will be executed in a function that receives the following parameters: + * <ol> + * <li>attrs - the AjaxRequestAttributes as JSON</li> + * <li>jqXHR - the jQuery XMLHttpRequest object</li> + * <li>settings - the settings used for the jQuery.ajax() call</li> + * </ol> + * * @param component * the Component with the Ajax behavior * @return the JavaScript that will be executed before the Ajax call. @@ -56,6 +79,12 @@ public interface IAjaxCallListener extends IClusterable CharSequence getBeforeHandler(Component component); /** + * The JavaScript that will be executed after the Ajax call. + * The script will be executed in a function that receives the following parameters: + * <ol> + * <li>attrs - the AjaxRequestAttributes as JSON</li> + * </ol> + * * @param component * the Component with the Ajax behavior * @return the JavaScript that will be executed after the start of the Ajax call @@ -67,6 +96,13 @@ public interface IAjaxCallListener extends IClusterable CharSequence getAfterHandler(Component component); /** + * The JavaScript that will be executed after both successful and unsuccessful return of the Ajax call. + * The script will be executed in a function that receives the following parameters: + * <ol> + * <li>jqXHR - the jQuery XMLHttpRequest object</li> + * <li>textStatus - the status as text</li> + * <li>attrs - the AjaxRequestAttributes as JSON</li> + * </ol> * @param component * the Component with the Ajax behavior * @return the JavaScript that will be executed after both successful http://git-wip-us.apache.org/repos/asf/wicket/blob/aa886f40/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/JavaScriptPrecondition.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/JavaScriptPrecondition.java b/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/JavaScriptPrecondition.java index f39a716..3ac6266 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/JavaScriptPrecondition.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/JavaScriptPrecondition.java @@ -17,7 +17,14 @@ package org.apache.wicket.ajax.attributes; /** - * + * The JavaScript that will be executed before the Ajax call and will decide whether the Ajax + * should be aborted. + * The script will be executed in a function that receives the following parameters: + * <ol> + * <li>attrs - the AjaxRequestAttributes as JSON</li> + * <li>jqXHR - the jQuery XMLHttpRequest object</li> + * <li>settings - the settings used for the jQuery.ajax() call</li> + * </ol> */ public class JavaScriptPrecondition extends JavaScriptFunctionBody { http://git-wip-us.apache.org/repos/asf/wicket/blob/aa886f40/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js index 92ae821..ffd3b92 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js @@ -446,7 +446,7 @@ url: attrs.u, type: attrs.m || 'GET', context: self, - beforeSend: function () { + beforeSend: function (jqXHR, settings) { var preconditions = attrs.pre || defaultPrecondition; if (jQuery.isArray(preconditions)) { @@ -457,7 +457,7 @@ if (jQuery.isFunction(precondition)) { result = precondition(); } else { - result = new Function('attrs', precondition)(attrs); + result = new Function('attrs', 'jqXHR', 'settings', precondition)(attrs, jqXHR, settings); } if (result === false) { Wicket.Log.info("Ajax request stopped because of precondition check, url: " + attrs.u); @@ -467,7 +467,7 @@ } } - self._executeHandlers(attrs.bh); + self._executeHandlers(attrs.bh, attrs, jqXHR, settings); if (attrs.i) { // show the indicator @@ -484,13 +484,13 @@ if (attrs.wr) { self.processAjaxResponse(data, textStatus, jqXHR, attrs); } else { - self._executeHandlers(attrs.sh, data, textStatus, jqXHR); + self._executeHandlers(attrs.sh, data, textStatus, jqXHR, attrs); } }, error: function(jqXHR, textStatus, errorThrown) { - self.failure(errorThrown, attrs); + self.failure(errorThrown, attrs, jqXHR, textStatus); }, complete: function (jqXHR, textStatus) { @@ -498,12 +498,14 @@ Wicket.DOM.hide(attrs.i); } + self._executeHandlers(attrs.coh, jqXHR, textStatus, attrs); + this.done(); } }); // execute after handlers right after the Ajax request is fired - self._executeHandlers(attrs.ah); + self._executeHandlers(attrs.ah, attrs); var allowDefault = attrs.ad || false; @@ -756,7 +758,7 @@ steps.push(jQuery.proxy(function (notify) { Wicket.Log.info("Response processed successfully."); - this._executeHandlers(attrs.sh); + this._executeHandlers(attrs.sh, null, 'success', null, attrs); Wicket.Ajax.invokePostCallHandlers(); // retach the events to the new components (a bit blunt method...) @@ -779,7 +781,7 @@ if (message) { Wicket.Log.error("Wicket.Ajax.Call.failure: Error while parsing response: " + message); } - this._executeHandlers(attrs.fh); + this._executeHandlers(attrs.fh, attrs); Wicket.Ajax.invokePostCallHandlers(); Wicket.Ajax.invokeFailureHandlers(); }, http://git-wip-us.apache.org/repos/asf/wicket/blob/aa886f40/wicket-core/src/test/js/ajax.js ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/js/ajax.js b/wicket-core/src/test/js/ajax.js index 470b858..3a2b4c6 100644 --- a/wicket-core/src/test/js/ajax.js +++ b/wicket-core/src/test/js/ajax.js @@ -283,5 +283,109 @@ jQuery(document).ready(function() { target.off("event1"); }); + + asyncTest('Wicket.Ajax - verify arguments to IAjaxCallListener handlers. Success scenario.', function () { + + expect(11); + + var attrs = { + u: 'data/ajax/nonWicketResponse.json', + e: 'event1', + dt: 'json', // datatype + wr: false, // not Wicket's <ajax-response> + sh: [ + function(data, textStatus, jqXHR, attributes) { + start(); + var expected = { + one: 1, + two: '2', + three: true + }; + deepEqual(data, expected); + equal('success', textStatus); + deepEqual(attrs, attributes); + ok(jQuery.isFunction(jqXHR.getResponseHeader), 'Assert that jqXHR is a XMLHttpRequest'); + } + ], + fh: [ + function(attributes) { + ok(false, 'Should not be called'); + } + ], + bh: [ + function(attributes, jqXHR, settings) { + deepEqual(attrs, attributes); + ok(jQuery.isFunction(jqXHR.getResponseHeader), 'Assert that jqXHR is a XMLHttpRequest'); + ok(jQuery.isFunction(settings.beforeSend), 'Assert that settings is the object passed to jQuery.ajax()'); + } + ], + ah: [ + function(attributes) { + deepEqual(attrs, attributes); + } + ], + coh: [ + function(jqXHR, textStatus, attributes) { + ok(jQuery.isFunction(jqXHR.getResponseHeader), 'Assert that jqXHR is a XMLHttpRequest'); + equal('success', textStatus); + deepEqual(attrs, attributes); + } + ] + } + + Wicket.Ajax.ajax(attrs); + + var target = jQuery(window); + target.triggerHandler("event1"); + target.off("event1"); + }); + + asyncTest('Wicket.Ajax - verify arguments to IAjaxCallListener handlers. Failure scenario.', function () { + + expect(8); + + var attrs = { + u: 'data/ajax/nonExisting.json', + e: 'event1', + dt: 'json', // datatype + wr: false, // not Wicket's <ajax-response> + sh: [ + function(data, textStatus, jqXHR, attributes) { + ok(false, 'Should not be called'); + } + ], + fh: [ + function(attributes) { + start(); + deepEqual(attrs, attributes); + } + ], + bh: [ + function(attributes, jqXHR, settings) { + deepEqual(attrs, attributes); + ok(jQuery.isFunction(jqXHR.getResponseHeader), 'Assert that jqXHR is a XMLHttpRequest'); + ok(jQuery.isFunction(settings.beforeSend), 'Assert that settings is the object passed to jQuery.ajax()'); + } + ], + ah: [ + function(attributes) { + deepEqual(attrs, attributes); + } + ], + coh: [ + function(jqXHR, textStatus, attributes) { + ok(jQuery.isFunction(jqXHR.getResponseHeader), 'Assert that jqXHR is a XMLHttpRequest'); + equal('error', textStatus); + deepEqual(attrs, attributes); + } + ] + } + + Wicket.Ajax.ajax(attrs); + + var target = jQuery(window); + target.triggerHandler("event1"); + target.off("event1"); + }); } });
