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

...

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

...