You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2012/07/26 13:35:52 UTC

git commit: WICKET-4674 Add support for Ajax call listsners for multipart form submittion

Updated Branches:
  refs/heads/master 8776114b7 -> b23289746


WICKET-4674 Add support for Ajax call listsners for multipart form submittion


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/b2328974
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/b2328974
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/b2328974

Branch: refs/heads/master
Commit: b232897467f823a7a40f6efcba0fe3e8cda999f2
Parents: 8776114
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Thu Jul 26 14:34:54 2012 +0300
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Thu Jul 26 14:34:54 2012 +0300

----------------------------------------------------------------------
 .../wicket/ajax/AbstractDefaultAjaxBehavior.java   |    2 +-
 .../wicket/ajax/attributes/IAjaxCallListener.java  |    7 ++-
 .../wicket/ajax/res/js/wicket-ajax-jquery.js       |   65 +++++++++-----
 .../ajax/AbstractDefaultAjaxBehaviorTest.java      |    2 +-
 wicket-core/src/test/js/ajax.js                    |   61 ++++++++++++++
 5 files changed, 111 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/b2328974/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 6018ea7..9494d2b 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
@@ -62,7 +62,7 @@ public abstract class AbstractDefaultAjaxBehavior extends AbstractAjaxBehavior
 		AbstractDefaultAjaxBehavior.class, "indicator.gif");
 
 	private static final String DYNAMIC_PARAMETER_FUNCTION_TEMPLATE = "function(attrs){%s}";
-	private static final String PRECONDITION_FUNCTION_TEMPLATE      = "function(attrs, jqXHR, settings){%s}";
+	private static final String PRECONDITION_FUNCTION_TEMPLATE      = "function(attrs){%s}";
 	private static final String COMPLETE_HANDLER_FUNCTION_TEMPLATE  = "function(attrs, jqXHR, textStatus){%s}";
 	private static final String FAILURE_HANDLER_FUNCTION_TEMPLATE   = "function(attrs, jqXHR, errorMessage, textStatus){%s}";
 	private static final String SUCCESS_HANDLER_FUNCTION_TEMPLATE   = "function(attrs, jqXHR, data, textStatus){%s}";

http://git-wip-us.apache.org/repos/asf/wicket/blob/b2328974/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 534cc4e..48bc1fc 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
@@ -128,8 +128,13 @@ public interface IAjaxCallListener
 	 * A JavaScript function that is invoked before the request executes.
 	 * If it returns {@code false} then the execution of the Ajax call will
 	 * be cancelled.
+	 * The script will be executed in a function that receives the following parameters:
+	 * <ol>
+	 *     <li>attrs - the AjaxRequestAttributes as JSON</li>
+	 * </ol>
 	 *
-	 * @return
+	 * @return the JavaScript that should be used to decide whether the Ajax
+	 *      call should be made at all.
 	 */
 	CharSequence getPrecondition(Component component);
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/b2328974/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 77102ea..0899950 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
@@ -473,9 +473,27 @@
 			self._executeHandlers(attrs.bh, attrs);
 			Wicket.Event.publish('/ajax/call/before', attrs);
 
-			if (attrs.mp) { // multipart form. jQuery doesn't help here ...
-				// TODO Wicket.next - should we execute all handlers ?!
-				// Wicket 1.5 didn't support success/failure handlers for this, but we can do it
+			var preconditions = attrs.pre || [];
+			preconditions = defaultPrecondition.concat(preconditions);
+			if (jQuery.isArray(preconditions)) {
+				for (var p = 0; p < preconditions.length; p++) {
+
+					var precondition = preconditions[p];
+					var result;
+					if (jQuery.isFunction(precondition)) {
+						result = precondition(attrs);
+					} else {
+						result = new Function('attrs', precondition)(attrs);
+					}
+					if (result === false) {
+						Wicket.Log.info("Ajax request stopped because of precondition check, url: " + attrs.u);
+						self.done();
+						return false;
+					}
+				}
+			}
+
+			if (attrs.mp) { // multipart form. jQuery.ajax() doesn't help here ...
 				return this.submitMultipartForm(context);
 			}
 
@@ -506,26 +524,6 @@
 				context: self,
 				beforeSend: function (jqXHR, settings) {
 
-					var preconditions = attrs.pre || [];
-					preconditions = defaultPrecondition.concat(preconditions);
-					if (jQuery.isArray(preconditions)) {
-						for (var p = 0; p < preconditions.length; p++) {
-
-							var precondition = preconditions[p];
-							var result;
-							if (jQuery.isFunction(precondition)) {
-								result = precondition(attrs, jqXHR, settings);
-							} else {
-								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);
-								self.done();
-								return false;
-							}
-						}
-					}
-
 					// collect the dynamic extra parameters
 					if (jQuery.isArray(attrs.dep)) {
 						var deps = attrs.dep,
@@ -707,6 +705,15 @@
 		submitMultipartForm: function (context) {
 
 			var attrs = context.attrs;
+
+			this._executeHandlers(attrs.bsh, attrs, null, null);
+			Wicket.Event.publish('/ajax/call/beforeSend', attrs, null, null);
+
+			if (attrs.i) {
+				// show the indicator
+				Wicket.DOM.showIncrementally(attrs.i);
+			}
+
 			var form = Wicket.$(attrs.f);
 			if (!form) {
 				Wicket.Log.error("Wicket.Ajax.Call.submitForm: Trying to submit form with id '" + attrs.f + "' that is not in document.");
@@ -768,6 +775,9 @@
 			//submit the form into the iframe, response will be handled by the onload callback
 			form.submit();
 
+			this._executeHandlers(attrs.ah, attrs);
+			Wicket.Event.publish('/ajax/call/after', attrs);
+
 			// install handler to deal with the ajax response
 			// ... we add the onload event after form submit because chrome fires it prematurely
 			Wicket.Event.add(iframe, "load.handleMultipartComplete", jQuery.proxy(this.handleMultipartComplete, this), context);
@@ -810,6 +820,15 @@
 				jQuery('#'+iframe.id + '-btn').remove();
 				jQuery(iframe).remove();
 
+				var attrs = context.attrs;
+				if (attrs.i) {
+					// hide the indicator
+					Wicket.DOM.hideIncrementally(attrs.i);
+				}
+
+				this._executeHandlers(attrs.coh, attrs, null, null);
+				Wicket.Event.publish('/ajax/call/complete', attrs, null, null);
+
 				this.done();
 			}, this));
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/b2328974/wicket-core/src/test/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehaviorTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehaviorTest.java b/wicket-core/src/test/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehaviorTest.java
index bf947f5..01714e0 100644
--- a/wicket-core/src/test/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehaviorTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehaviorTest.java
@@ -65,7 +65,7 @@ public class AbstractDefaultAjaxBehaviorTest extends Assert
 
 		CharSequence json = behavior.renderAjaxAttributes(component, attributes);
 
-		String expected = "{\"coh\":[function(attrs, jqXHR, textStatus){alert('Complete!');}],\"u\":\"some/url\",\"pre\":[function(attrs, jqXHR, settings){return somePrecondition();}],\"fh\":[function(attrs, jqXHR, errorMessage, textStatus){alert('Failure!');}],\"bh\":[function(attrs){alert('Before!');}],\"sh\":[function(attrs, jqXHR, data, textStatus){alert('Success!');}],\"ah\":[function(attrs){alert('After!');}]}";
+		String expected = "{\"coh\":[function(attrs, jqXHR, textStatus){alert('Complete!');}],\"u\":\"some/url\",\"pre\":[function(attrs){return somePrecondition();}],\"fh\":[function(attrs, jqXHR, errorMessage, textStatus){alert('Failure!');}],\"bh\":[function(attrs){alert('Before!');}],\"sh\":[function(attrs, jqXHR, data, textStatus){alert('Success!');}],\"ah\":[function(attrs){alert('After!');}]}";
 		assertEquals(expected, json);
 	}
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/b2328974/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 164ccdf..e258f4a 100644
--- a/wicket-core/src/test/js/ajax.js
+++ b/wicket-core/src/test/js/ajax.js
@@ -929,5 +929,66 @@ jQuery(document).ready(function() {
 
 			jQuery('#'+ attrs.c).triggerHandler(attrs.e);
 		});
+
+
+		/**
+		 * Tests that Ajax call handlers are called when a (nested) multipart form
+		 * is submitted with Ajax.
+		 * The url points to Ajax response that contains an evaluation that starts the test.
+		 */
+		asyncTest('Submit nested form - success scenario.', function () {
+
+			expect(7);
+
+			var attrs = {
+				f:  "innerForm", // the id of the form to submit
+				mp: true,  // multipart
+				u:  "data/ajax/submitNestedForm.xml", // the mock response
+				e:  "nestedFormSubmit", // the event
+				c:  "innerSubmitButton", // the component that submits the form
+				m:  "POST", // submit method,
+				bh: [ function(attrs) { ok(true, "Before handler executed"); } ],
+				pre: [ function(attrs) {ok(true, "Precondition executed"); return true; } ],
+				bsh: [ function(attrs) { ok(true, "BeforeSend handler executed"); } ],
+				ah: [ function(attrs) { ok(true, "After handler executed"); } ],
+				sh: [ function(attrs) { ok(true, "Success handler executed"); } ],
+				fh: [ function(attrs) { ok(false, "Failure handler should not be executed"); } ],
+				coh: [ function(attrs) { ok(true, "Complete handler executed"); } ]
+			};
+
+			Wicket.Ajax.ajax(attrs);
+
+			jQuery('#'+ attrs.c).triggerHandler(attrs.e);
+		});
+
+		/**
+		 * Tests that Ajax call handlers are called when a (nested) multipart form
+		 * is submitted with Ajax.
+		 * Since the url points to not existing resource the final result is a failure.
+		 */
+		asyncTest('Submit nested form - failure scenario.', function () {
+
+			expect(6);
+
+			var attrs = {
+				f:  "innerForm", // the id of the form to submit
+				mp: true,  // multipart
+				u:  "data/ajax/submitNestedForm-NotExist.xml", // the mock response
+				e:  "nestedFormSubmit", // the event
+				c:  "innerSubmitButton", // the component that submits the form
+				m:  "POST", // submit method,
+				bh: [ function(attrs) { ok(true, "Before handler executed"); } ],
+				pre: [ function(attrs) {ok(true, "Precondition executed"); return true; } ],
+				bsh: [ function(attrs) { ok(true, "BeforeSend handler executed"); } ],
+				ah: [ function(attrs) { ok(true, "After handler executed"); } ],
+				sh: [ function(attrs) { ok(false, "Success handler should not be executed"); } ],
+				fh: [ function(attrs) { ok(true, "Failure handler executed"); start() } ],
+				coh: [ function(attrs) { ok(true, "Complete handler executed"); } ]
+			};
+
+			Wicket.Ajax.ajax(attrs);
+
+			jQuery('#'+ attrs.c).triggerHandler(attrs.e);
+		});
 	}
 });