wicket-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mgrigo...@apache.org
Subject [14/20] git commit: WICKET-5025 Set the component as a context to the ajax listeners
Date Fri, 08 Feb 2013 08:24:14 GMT
WICKET-5025 Set the component as a context to the ajax listeners


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

Branch: refs/heads/reference-guide
Commit: e9df9fbc93663039e78874b2504a0db0b842644a
Parents: 474667c
Author: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
Authored: Thu Feb 7 10:22:53 2013 +0100
Committer: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
Committed: Thu Feb 7 10:22:53 2013 +0100

----------------------------------------------------------------------
 .../wicket/ajax/res/js/wicket-ajax-jquery.js       |   36 +++++++-
 wicket-core/src/test/js/ajax.js                    |   66 +++++++++++++++
 wicket-core/src/test/js/all.html                   |    6 ++
 3 files changed, 104 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/e9df9fbc/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 71d10fb..df32e12 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
@@ -365,6 +365,27 @@
 		},
 
 		/**
+		 * Extracts the HTML element that "caused" this Ajax call.
+		 * An Ajax call is usually caused by JavaScript event but maybe be also
+		 * caused by manual usage of the JS API..
+		 *
+		 * @param attrs {Object} - the ajax request attributes
+		 * @return {HTMLElement} - the DOM element
+		 * @private
+		 */
+		_getTarget: function (attrs) {
+			var target;
+			if (attrs.event) {
+				target = attrs.event.target;
+			} else if (!jQuery.isWindow(attrs.c)) {
+				target = Wicket.$(attrs.c);
+			} else {
+				target = window;
+			}
+			return target;
+		},
+
+		/**
 		 * A helper function that executes an array of handlers (before, success, failure)
 		 *
 		 * @param handlers {Array[Function]} - the handlers to execute
@@ -376,12 +397,16 @@
 				// cut the handlers argument
 				var args = Array.prototype.slice.call(arguments).slice(1);
 
+				// assumes that the Ajax attributes is always the first argument
+				var attrs = args[0];
+				var that = this._getTarget(attrs);
+
 				for (var i = 0; i < handlers.length; i++) {
 					var handler = handlers[i];
 					if (jQuery.isFunction(handler)) {
-						handler.apply(this, args);
+						handler.apply(that, args);
 					} else {
-						new Function(handler).apply(this, args);
+						new Function(handler).apply(that, args);
 					}
 				}
 			}
@@ -480,14 +505,17 @@
 			var preconditions = attrs.pre || [];
 			preconditions = defaultPrecondition.concat(preconditions);
 			if (jQuery.isArray(preconditions)) {
+
+				var that = this._getTarget(attrs);
+
 				for (var p = 0; p < preconditions.length; p++) {
 
 					var precondition = preconditions[p];
 					var result;
 					if (jQuery.isFunction(precondition)) {
-						result = precondition(attrs);
+						result = precondition.call(that, attrs);
 					} else {
-						result = new Function('attrs', precondition)(attrs);
+						result = new Function(precondition).call(that, attrs);
 					}
 					if (result === false) {
 						Wicket.Log.info("Ajax request stopped because of precondition check, url: " + attrs.u);

http://git-wip-us.apache.org/repos/asf/wicket/blob/e9df9fbc/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 cd60258..f603abc 100644
--- a/wicket-core/src/test/js/ajax.js
+++ b/wicket-core/src/test/js/ajax.js
@@ -1047,5 +1047,71 @@ jQuery(document).ready(function() {
 
 			jQuery(window).triggerHandler("manyEvaluations");
 		});
+
+		/**
+		 * The DOM elememt of the HTML element is used as a context (this)
+		 * in the callbacks.
+		 * https://issues.apache.org/jira/browse/WICKET-5025
+		 */
+		asyncTest('The HTML DOM element should be the context in the callbacks - success case.',
function () {
+
+			expect(6);
+
+			var attrs = {
+				u: 'data/ajax/emptyAjaxResponse.xml',
+				c: 'usedAsContextWicket5025',
+				e: 'asContextSuccess',
+				bh: [ function() { equal(this.id, 'usedAsContextWicket5025', "Before handler executed");
} ],
+				pre: [ function() { equal(this.id, 'usedAsContextWicket5025', "Precondition executed");
return true; } ],
+				bsh: [ function() { equal(this.id, 'usedAsContextWicket5025', "BeforeSend handler executed");
} ],
+				ah: [ function() { equal(this.id, 'usedAsContextWicket5025', "After handler executed");
} ],
+				sh: [ function() { equal(this.id, 'usedAsContextWicket5025', "Success handler executed");
} ],
+				fh: [ function() { ok(false, "Failure handler should not be executed"); } ],
+				coh: [
+					function() {
+						equal(this.id, 'usedAsContextWicket5025', "Complete handler executed");
+						jQuery('#usedAsContextWicket5025').off();
+						start();
+					}
+				]
+			};
+
+			Wicket.Ajax.ajax(attrs);
+
+			jQuery('#usedAsContextWicket5025').triggerHandler("asContextSuccess");
+		});
+
+		/**
+		 * The DOM elememt of the HTML element is used as a context (this)
+		 * in the callbacks.
+		 * https://issues.apache.org/jira/browse/WICKET-5025
+		 */
+		asyncTest('The HTML DOM element should be the context in the callbacks - failure case.',
function () {
+
+			expect(6);
+
+			var attrs = {
+				u: 'data/ajax/nonExisting.xml',
+				c: 'usedAsContextWicket5025',
+				e: 'asContextFailure',
+				bh: [ function() { equal(this.id, 'usedAsContextWicket5025', "Before handler executed");
} ],
+				pre: [ function() { equal(this.id, 'usedAsContextWicket5025', "Precondition executed");
return true; } ],
+				bsh: [ function() { equal(this.id, 'usedAsContextWicket5025', "BeforeSend handler executed");
} ],
+				ah: [ function() { equal(this.id, 'usedAsContextWicket5025', "After handler executed");
} ],
+				sh: [ function() { ok(false, "Success handler should not be executed"); } ],
+				fh: [ function() { equal(this.id, 'usedAsContextWicket5025', "Failure handler should
not be executed"); } ],
+				coh: [
+					function() {
+						equal(this.id, 'usedAsContextWicket5025', "Complete handler executed");
+						jQuery('#usedAsContextWicket5025').off();
+						start();
+					}
+				]
+			};
+
+			Wicket.Ajax.ajax(attrs);
+
+			jQuery('#usedAsContextWicket5025').triggerHandler("asContextFailure");
+		});
 	}
 });

http://git-wip-us.apache.org/repos/asf/wicket/blob/e9df9fbc/wicket-core/src/test/js/all.html
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/js/all.html b/wicket-core/src/test/js/all.html
index 07fc403..a6f4d52 100644
--- a/wicket-core/src/test/js/all.html
+++ b/wicket-core/src/test/js/all.html
@@ -12,6 +12,10 @@
 	<script type="text/javascript" src="qunit/qunit.js"></script>
 
 	<!-- the module under test -->
+    <script>
+        // uncomment for error reporting from the tests
+//        Wicket.Ajax.DebugWindow.enabled = true;
+    </script>
 	<script type="text/javascript" src="channels.js"></script>
 	<script type="text/javascript" src="dom.js"></script>
 	<script type="text/javascript" src="event.js"></script>
@@ -160,6 +164,8 @@
 			</div>
 		</form>
 
+        <div id="usedAsContextWicket5025"></div>
+
 	</div>
 </body>
 </html>


Mime
View raw message