Maximilian Reichel created WICKET-7055:
------------------------------------------

             Summary: AjaxEventBehavior onload event unstable
                 Key: WICKET-7055
                 URL: https://issues.apache.org/jira/browse/WICKET-7055
             Project: Wicket
          Issue Type: Bug
          Components: wicket-core
            Reporter: Maximilian Reichel


As today the AjaxEventBehavior class is not compatible to be used with the 
onload event but I could not find this to be documented anywhere.

When using the AjaxEventBehavior class to hook into the onload event, it works 
sometimes but it is not stable. This is because AjaxEventBehavior class itself 
uses the dom ready event to add the user-defined hooks to the page, but as 
clarified [here|https://github.com/jquery/jquery/issues/5075], the jQuery dom 
ready event may fires later than the onload event. In this instance the 
AjaxEventBehavior adds the user-defined onload hook after the onload event was 
already fired and therefore the user-defined hook is never executed.

As a workaround (tested with wicket 6.18.0) I wrote this class which directly 
hooks onto the onload event:
{code:java}
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.OnLoadHeaderItem;

public abstract class AjaxOnLoadEventBehavior extends 
AbstractDefaultAjaxBehavior {

    @Override
    public void renderHead(final Component component, final IHeaderResponse 
response) {
        super.renderHead(component, response);

        if (component.isEnabledInHierarchy())
        {
            CharSequence js = getCallbackScript(component);

            response.render(OnLoadHeaderItem.forScript(js.toString()));
        }
    }

    @Override
    protected void respond(AjaxRequestTarget target) {
        onLoad(target);
    }

    protected abstract void onLoad(final AjaxRequestTarget target);
}
{code}

In my opinion this should be added to the documentation and the 
AjaxEventBehavior class should log a warning when used with the onload event. 
Maybe wicket could include a class like my AjaxOnLoadEventBehavior.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to