Search code examples
javascriptjavawicket

Always render certain Wicket HeaderItem after all other HeaderItems of specific type


I have a custom Wicket Behaviour (RegisterBehaviour) which can be attached to various Wicket components to add multiple JaveScript-Snippets event handlers to them via a (OnDomReady-)HeaderItem in RegisterBehaviour.renderHead(Component, IHeaderResponse).

Now I have the requirement to trigger one of these events right after the page has been loaded to initialise all components with a certain computed value. To do that, I thought could add another Wicket Behaviour (TriggerBehaviour) which generates the approriate JaveScript-Snippet.

My problem is though, that the JaveScript-Snippet generated by this TriggerBehaviour would have to be run after the JaveScript-Snippets of all the RegisterBehaviour on a page and in all components have been run. I know that HeaderItems can have dependencies but this mechanism seems to target the case, when I want some kind of common library loaded (like jQuery) or code executed before my custom code. To use this for my case, I'd have to somehow find all RegisterBehaviours on the fly that are being added to the current render phase of the page and dynamicly add them as dependencies to the TriggerBehaviour, so they run before the TriggerBehaviours JavaScript. I see no way though, how to get such a list of all HeaderItems of a certain type.

Any tips on how to approach this problem, either in Wicket or JavaScript?

(Using Java 8 and Wicket 8.9.0)


Solution

  • You can use FilteredHeaderItem.

    Both RegisterBehaviour and TriggerBehaviour will contribute their header items wrapped in FilteredHeaderItems.

    In your HTML template you will use two placeholders for the two filters:

    <div id="registerFilter"></div>
    <div id="triggerFilter"></div>
    

    The browsers execute the JavaScripts from top to bottom so the trigger ones will be after the register ones.