Wicket AjaxPage edited by Geoff HaymanChanges (2)
Full Content
What ?Since version 6.0 Wicket uses JQuery as a backing library for its Ajax functionality. Why ?The previous implementations of wicket-ajax.js and wicket-event.js were home baked solutions that worked well but also suffered from the differences in the browsers. Often users complained that some functionality doesn't work on particular version of particular browser. That's why the Wicket team chose to use JQuery to deal with browser inconsistencies and leave us to do our business logic. Design and implementationThe new implementations (wicket-ajax-jquery.js and wicket-event-jquery.js) use JQuery internally but expose Wicket.** API similar to the previous version. All Java components and behaviors should still use the Wicket.** API. This way if someday we decide to not use JQuery anymore we will have less work to do. Also if a user uses Dojo/YUI/ExtJS/... and prefer to not have JQuery in her application then she will be able to provide wicket-ajax-xyz.js implementation and replace the default one. Table with renamed methods from the previous version
ConfigurationSetupTo replace any of the _javascript_ files the user application may use: MyApplication#init(): public void init() { super.init(); IJavaScriptLibrarySettings jsSettings = getJavaScriptLibrarySettings(); jsSettings.setJQueryReference(new MyJQueryReference()); jsSettings.setWicketEventReference(new DojoWicketEventReference()); jsSettings.setWicketAjaxReference(new DojoWicketAjaxReference()); } Resource dependenciesSince Wicket 6.0 ResourceReference can have dependencies and it is recommended to properly define the dependency chain between this classes. If the user application needs to upgrade/downgrade to new/old version of JQuery then just the first line above is needed:
getJavaScriptLibrarySettings().setJQueryReference(new AnotherVersionOfJQueryReference());
If the user application needs to use Dojo instead of JQuery then it has provide _javascript_ResourceReferences for wicket-event-dojo.js and wicket-ajax-dojo.js (e.g. DojoWicketEventReference and DojoWicketAjaxReference). Those references should define dependency to DojoReference (a reference that delivers dojo.js). Wicket uses IJavaScriptLibrarySettings#getWicketAjaxReference() and all its transitive dependencies for its _javascript_ needs. AjaxRequestAttributesEach Ajax behavior and component can use o.a.w.ajax.attributes.AjaxRequestAttributes to configure how exactly the Ajax call should be executed and how its response should be handled. To do this use: AnyAjaxComponent/AnyAjaxBehavior.java: protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { super.updateAjaxAttributes(AjaxRequestAttributes attributes); attributes.[set some attribute](); } The available attributes are:
Attributes 'c' (component id) and 'u' (callback url) are automatically set by the Ajax behavior and they are not part of AjaxRequestAttributes. While constructing the _javascript_ that will register the event listener for that Ajax component/behavior these settings are serialized to optimized JSON object which is passed as a parameter to Wicket.Ajax.(get|post|ajax) methods. Wicket.Ajax.get({"u":"the/url/to/the/link", "e": "click", "c":"linkId"}); Many of the attributes have default values which are not written in the JSON settings and they are initialized at the client side (i.e. wicket-ajax.js knows the defaults). The example above can be read as: when HTML element with id 'linkId' is clicked fire an Ajax call with Url 'the/url/to/the/link'. Migration stepso.a.w.ajax.IAjaxCallDecorator is replaced with o.a.w.ajax.attributes.IAjaxCallListener.Since Wicket Ajax now register DOM events (like click, change, ...) instead of using inline attributes like onclick, onchange, ... there is no more a script to decorate. Instead the new implementation provides points to listen to:
To use it do: protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { super.updateAjaxAttributes(attributes); AjaxCallListener myAjaxCallListener = new AjaxCallListener() { @Override public CharSequence getBeforeHandler(Component component) { return "alert('I\'m executed before the firing of the Ajax call')"; } }; attributes.getAjaxCallListeners().add(myAjaxCallListener); } There are also handy methods like onBefore(CharSequence), onComplete(CharSequence), ... but they do not provide access to the component which is bound with the Ajax behavior. An Ajax request can have 0 or more IAjaxCallListener's. Global Ajax call listenersIAjaxCallListener's can be used to listen for the lifecycle of an Ajax call for a specific component.
Those replaces the old Wicket.Ajax.(registerPreCallHandler|registerPostCallHandler|registerFailureHandler) methods and uses publish/subscribe mechanism. Example (_javascript_):
Wicket.Event.subscribe('/ajax/call/failure', function(jqEvent, attributes, jqXHR, errorThrown, textStatus) {
// do something when an Ajax call fails
});
All global listeners receive the same arguments as the respective IAjaxCallListener handler plus jQuery.Event that is passed by the PubSub system. This event is not the event that caused the Ajax call. The one that caused the Ajax call is 'attrs.event'. There are two additional topics which are used to notify the subscribers when an HTML element is about to be removed and when a new one is added to the document:
Automatically migrated attributes.Some of the attributes are available from the previous versions of Wicket as an overridable methods in AbstractDefaultAjaxBehavior. These methods are marked as deprecated and will be removed in Wicket 7.0 and for now Wicket automatically translates them into AjaxRequestAttributes.
It is recommended to override org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#updateAjaxAttributes(AjaxRequestAttributes) and configure those directly in the passed 'attributes'. Tests for the client side Wicket Ajax functionalityAt ajax.js you may see the currently available _javascript_ unit tests that we have for the Ajax functionality in wicket-ajax.js Blog articlesAn introduction to the new functionalities are described at Wicket In Action. There is also a link to a demo application. FAQHow to check whether my custom version of the backing _javascript_ library (jQuery) doesn't break Wicket internals somehow ?
What parameters are passed to the handlers ?
The global listeners receive the same parameters prepended by jqEvent. This is the event triggered by jQuery. See section Global Ajax call listeners above. How to use the preconditions with non-native/_javascript_-based confirm dialogs ?See possible solutions from a mailing list discussion
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Wicket > Wicket Ajax confluence
- [CONF] Apache Wicket > Wicket Ajax confluence
- [CONF] Apache Wicket > Wicket Ajax confluence
- [CONF] Apache Wicket > Wicket Ajax confluence