0
votes

I want a theoretical explanation about AjaxRequestTarget on wicket. Talking about behaviors: to create your own you must override onUpdate method, and can override other methods like onEvent, onBind etc.. but why AjaxRequestTarget object is only available in onUpdate method?

I want it in other methods, (specifically in onEvent()) because I want to validade form components. In my tests, onUpdate method not works as I want.. onUpdate (with focusout event) is being called only when model is changed... so it is called when I fill a TextField, for example, but not when it get's empty again..

onEvent method is called on both moments, but I can't update other components (like a label to display the feedback message to that field), because AjaxRequestTarget is not available on it.

1

1 Answers

1
votes

AjaxFormComponentUpdatingBehavior#onUpdate() is called only when an Ajax request is made!

AjaxEventBehavior#onEvent() is called only when an Ajax request is made!

Component#onEvent() is called when some (other, most of the time) Component calls Component#send(Sink, Broadcast, Payload). This might happen in both Ajax and normal/non-Ajax requests. Thus AjaxRequestTarget is not used in the API.

Usually the AjaxRequestTarget is passed as part of the Payload by the caller, in case this is done in Ajax request. This way the receiver (the Component that overrides Component#onEvent()) may use it if it need so:

Caller.java:

send(getPage(), Broadcast.BREADTH, new MySpecialPayload(target));

Receiver.java:

@Override public void onEvent(IEvent event) {
    Object payload = event.getPayload();
    if (payload instanceOf MySpecialPayload) {
        MySpecialPayload mySpecialPayload = (MySpecialPayload) payload;
        if (doSomethingWith(mySpecialPayload) {
           AjaxRequestTarget target = mySpecialPayload.getTarget();
           target.add(this);
        }
    }
}

Another way to get a reference to the AjaxRequestTarget from anywhere is: RequestCycle.get().find(AjaxRequestTarget.class). If it is non-null then this is an Ajax request and you can add components to the target, pre/append JavaScript, etc.