Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added /ajax/call/init and done

Table of Contents
minLevel3

What ?

Since version 6.0 Wicket uses jQuery as a backing library for its Ajax functionality.

...

MyApplication#init():

Code Block

public void init() {
  super.init();

  IJavaScriptLibrarySettings jsSettings = getJavaScriptLibrarySettings();

  jsSettings.setJQueryReference(new MyJQueryReference());

  jsSettings.setWicketEventReference(new DojoWicketEventReference());

  jsSettings.setWicketAjaxReference(new DojoWicketAjaxReference());

}

...

If the user application needs to upgrade/downgrade to new/old version of jQuery then just the first line above is needed:

Code Block

  getJavaScriptLibrarySettings().setJQueryReference(new AnotherVersionOfJQueryReference());

...

AnyAjaxComponent/AnyAjaxBehavior.java:

Code Block

protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
{
    super.updateAjaxAttributes(AjaxRequestAttributes attributes);

    attributes.[set some attribute]();
}

...

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.
For example an AjaxLink contributes JavaScript similar to :

Code Block

  Wicket.Ajax.get({"u":"the/url/to/the/link", "e": "click", "c":"linkId"});

...

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:

  • before handler - executed before the data for the Ajax call is calculated. Even before the preconditions.
  • precondition - if it returns false then the Ajax call (and all handlers below) is not executed at all
  • beforeSend handler - executed before the actual execution of the Ajax call.
  • after handler - if the Ajax call is asynchronous then it is executed right after its firing. If it is synchronous then it is executed after the complete handler
  • success handler - executed on successful return of the Ajax call
  • failure handler - executed on unsuccessful return of the Ajax call
  • complete handler - executed at the very end. After either the success or failure handlers.

To use it do:
AnyAjaxComponent/AnyAjaxBehavior.java:

Code Block

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);
}

...

IAjaxCallListener's can be used to listen for the lifecycle of an Ajax call for a specific component.
If the user application needs to listen for all Ajax calls then it may subscribe to the following topics:

  • /ajax/call/init
  • /ajax/call/before
  • /ajax/call/precondition
  • /ajax/call/beforeSend
  • /ajax/call/after
  • /ajax/call/success
  • /ajax/call/failure
  • /ajax/call/complete
  • /ajax/call/done

Those replaces the old Wicket.Ajax.(registerPreCallHandler|registerPostCallHandler|registerFailureHandler) methods and uses publish/subscribe mechanism.

Example (JavaScript):

Code Block

Wicket.Event.subscribe('/ajax/call/failure', function(jqEvent, attributes, jqXHR, errorThrown, textStatus) {
  // do something when an Ajax call fails
});

...

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:

  • /dom/node/removing - receives as parameters the jQuery.Event and the HTMLElement that will be removed
  • /dom/node/added - receives as parameters the jQuery.Event and the HTMLElement that has just been added

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.
These methods are:

  • org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getPreconditionScript()
  • org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getSuccessScript()
  • org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getFailureScript()
  • org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getChannel()

It is recommended to override org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#updateAjaxAttributes(AjaxRequestAttributes) and configure those directly in the passed 'attributes'.

...