Uploaded image for project: 'Wicket'
  1. Wicket
  2. WICKET-7055

AjaxEventBehavior onload event unstable

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • None
    • 10.0.0-M1, 9.14.0
    • wicket-core
    • None

    Description

      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, 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:

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

      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.

      Attachments

        Issue Links

          Activity

            People

              mgrigorov Martin Tzvetanov Grigorov
              reichemn Maximilian Reichel
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: