Updated Branches: refs/heads/master 680d6cd69 -> 19985702b
WICKET-3367 Rewrite all JavaScript inline event handlers to be proper attached event handlers Publish events on interesting Ajax events - before, after, success, failure and complete. This replaces the old register(Pre|Post)CallHandlers() Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/19985702 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/19985702 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/19985702 Branch: refs/heads/master Commit: 19985702b39ba76f733eb85f6d6a923f852490be Parents: 680d6cd Author: martin-g <[email protected]> Authored: Fri Jan 6 12:26:28 2012 +0200 Committer: martin-g <[email protected]> Committed: Fri Jan 6 12:26:28 2012 +0200 ---------------------------------------------------------------------- .../wicket/ajax/res/js/wicket-ajax-jquery.js | 91 +------------ wicket-core/src/test/js/ajax.js | 103 +++++++++++++++ 2 files changed, 110 insertions(+), 84 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/19985702/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 ef1ea7d..7477e51 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 @@ -483,6 +483,7 @@ } } + Wicket.Event.publish('/ajax/call/before', attrs, jqXHR, settings); self._executeHandlers(attrs.bh, attrs, jqXHR, settings); if (attrs.i) { @@ -502,11 +503,13 @@ } else { self._executeHandlers(attrs.sh, data, textStatus, jqXHR, attrs); } + Wicket.Event.publish('/ajax/call/success', data, textStatus, jqXHR, attrs); }, error: function(jqXHR, textStatus, errorThrown) { self.failure(errorThrown, attrs, jqXHR, textStatus); + Wicket.Event.publish('/ajax/call/failure', errorThrown, attrs, jqXHR, textStatus); }, complete: function (jqXHR, textStatus) { @@ -515,6 +518,7 @@ } self._executeHandlers(attrs.coh, jqXHR, textStatus, attrs); + Wicket.Event.publish('/ajax/call/complete', jqXHR, textStatus, attrs); this.done(); } @@ -522,6 +526,7 @@ // execute after handlers right after the Ajax request is fired self._executeHandlers(attrs.ah, attrs); + Wicket.Event.publish('/ajax/call/after', attrs); if (!attrs.ad && attrs.event) { attrs.event.preventDefault(); @@ -647,9 +652,6 @@ form.appendChild($btn[0]); } - // invoke pre call handlers - Wicket.Ajax.invokePreCallHandlers(); - //submit the form into the iframe, response will be handled by the onload callback form.submit(); @@ -774,8 +776,7 @@ this._executeHandlers(attrs.sh, null, 'success', null, attrs); - Wicket.Ajax.invokePostCallHandlers(); - // retach the events to the new components (a bit blunt method...) + // re-attach the events to the new components (a bit blunt method...) // This should be changed for IE See comments in wicket-event.js add (attachEvent/detachEvent) // IE this will cause double events for everything.. (mostly because of the jQuery.proxy(element)) Wicket.Focus.attachFocusEvent(); @@ -796,8 +797,6 @@ Wicket.Log.error("Wicket.Ajax.Call.failure: Error while parsing response: " + message); } this._executeHandlers(attrs.fh, attrs); - Wicket.Ajax.invokePostCallHandlers(); - Wicket.Ajax.invokeFailureHandlers(); }, done: function () { @@ -1372,62 +1371,13 @@ }, /** - * The Ajax class handles low level details of creating and pooling XmlHttpRequest objects, + * The Ajax class handles low level details of creating XmlHttpRequest objects, * as well as registering and execution of pre-call, post-call and failure handlers. */ Ajax: { Call: Wicket.Ajax.Call, - preCallHandlers: [], - postCallHandlers: [], - failureHandlers: [], - - registerPreCallHandler: function (handler) { - var h = Wicket.Ajax.preCallHandlers; - h.push(handler); - }, - - registerPostCallHandler: function (handler) { - var h = Wicket.Ajax.postCallHandlers; - h.push(handler); - }, - - registerFailureHandler: function (handler) { - var h = Wicket.Ajax.failureHandlers; - h.push(handler); - }, - - invokePreCallHandlers: function () { - var h = Wicket.Ajax.preCallHandlers; - if (h.length > 0) { - Wicket.Log.info("Invoking pre-call handler(s)..."); - } - for (var i = 0; i < h.length; ++i) { - h[i](); - } - }, - - invokePostCallHandlers: function () { - var h = Wicket.Ajax.postCallHandlers; - if (h.length > 0) { - Wicket.Log.info("Invoking post-call handler(s)..."); - } - for (var i = 0; i < h.length; ++i) { - h[i](); - } - }, - - invokeFailureHandlers: function () { - var h = Wicket.Ajax.failureHandlers; - if (h.length > 0) { - Wicket.Log.info("Invoking failure handler(s)..."); - } - for (var i = 0; i < h.length; ++i) { - h[i](); - } - }, - get: function (attrs) { attrs.m = 'GET'; @@ -2346,31 +2296,4 @@ Wicket.Event.add(window, 'domready', Wicket.Focus.attachFocusEvent); - Wicket.Ajax.registerPreCallHandler(function () { - if (typeof(window.wicketGlobalPreCallHandler) !== "undefined") { - var global = window.wicketGlobalPreCallHandler; - if (global !== null) { - global(); - } - } - }); - - Wicket.Ajax.registerPostCallHandler(function () { - if (typeof(window.wicketGlobalPostCallHandler) !== "undefined") { - var global = window.wicketGlobalPostCallHandler; - if (global !== null) { - global(); - } - } - }); - - Wicket.Ajax.registerFailureHandler(function () { - if (typeof(window.wicketGlobalFailureHandler) !== "undefined") { - var global = window.wicketGlobalFailureHandler; - if (global !== null) { - global(); - } - } - }); - })(); http://git-wip-us.apache.org/repos/asf/wicket/blob/19985702/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 4f0509c..c877462 100644 --- a/wicket-core/src/test/js/ajax.js +++ b/wicket-core/src/test/js/ajax.js @@ -431,5 +431,108 @@ jQuery(document).ready(function() { Wicket.Ajax.ajax(attrs); }); + + asyncTest('Wicket.Ajax - verify arguments to global listeners. Success scenario.', function () { + + expect(11); + + var attrs = { + u: 'data/ajax/nonWicketResponse.json', + e: 'event1', + dt: 'json', // datatype + wr: false // not Wicket's <ajax-response> + }; + + Wicket.Event.subscribe('/ajax/call/success', function(jqEvent, data, textStatus, jqXHR, attributes) { + start(); + var expected = { + one: 1, + two: '2', + three: true + }; + deepEqual(data, expected, 'data'); + equal('success', textStatus, 'textStatus'); + deepEqual(attrs, attributes, 'attrs'); + ok(jQuery.isFunction(jqXHR.getResponseHeader), 'Assert that jqXHR is a XMLHttpRequest'); + }); + + Wicket.Event.subscribe('/ajax/call/failure', function(jqEvent, attributes) { + ok(false, 'Failure handler should not be called'); + }); + + Wicket.Event.subscribe('/ajax/call/before', function(jqEvent, attributes, jqXHR, settings) { + deepEqual(attrs, attributes, 'attrs'); + 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()'); + }); + + Wicket.Event.subscribe('/ajax/call/after', function(jqEvent, attributes) { + deepEqual(attrs, attributes, 'attrs'); + }); + + Wicket.Event.subscribe('/ajax/call/complete', function(jqEvent, jqXHR, textStatus, attributes) { + ok(jQuery.isFunction(jqXHR.getResponseHeader), 'Assert that jqXHR is a XMLHttpRequest'); + equal('success', textStatus, 'textStatus'); + deepEqual(attrs, attributes, 'attrs'); + }); + + Wicket.Ajax.ajax(attrs); + + var target = jQuery(window); + target.triggerHandler("event1"); + target.off("event1"); + + // unregister all subscribers + jQuery(document).off(); + }); + + asyncTest('Wicket.Ajax - verify arguments to global listeners. Failure scenario.', function () { + + expect(11); + + var attrs = { + u: 'data/ajax/nonExisting.json', + e: 'event1', + dt: 'json', // datatype + wr: false // not Wicket's <ajax-response> + }; + + Wicket.Event.subscribe('/ajax/call/success', function(jqEvent, data, textStatus, jqXHR, attributes) { + ok(false, 'Success handles should not be called'); + }); + + Wicket.Event.subscribe('/ajax/call/failure', function(jqEvent, errorThrown, attributes, jqXHR, textStatus) { + start(); + ok('Not Found', errorThrown); + ok(jQuery.isFunction(jqXHR.getResponseHeader), 'Assert that jqXHR is a XMLHttpRequest'); + equal('error', textStatus, 'textStatus'); + deepEqual(attrs, attributes, 'attrs'); + }); + + Wicket.Event.subscribe('/ajax/call/before', function(jqEvent, attributes, jqXHR, settings) { + deepEqual(attrs, attributes, 'attrs'); + 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()'); + }); + + Wicket.Event.subscribe('/ajax/call/after', function(jqEvent, attributes) { + deepEqual(attrs, attributes, 'attrs'); + }); + + Wicket.Event.subscribe('/ajax/call/complete', function(jqEvent, jqXHR, textStatus, attributes) { + ok(jQuery.isFunction(jqXHR.getResponseHeader), 'Assert that jqXHR is a XMLHttpRequest'); + equal('error', textStatus, 'textStatus'); + deepEqual(attrs, attributes, 'attrs'); + }); + + Wicket.Ajax.ajax(attrs); + + var target = jQuery(window); + target.triggerHandler("event1"); + target.off("event1"); + + // unregister all subscribers + jQuery(document).off(); + }); } });
