Pergunta

Working in Wicket 6. My page includes a javascript reference in the html to a third party library. I have a block of additional standard utils that I include via JavaScriptHeaderItem in a renderHead in the Behavior class from a custom component.

Now, that same component needs to write some specific javascript code inline to call methods from those two includes. The problem is that the code is running before even simple variables within those includes are loaded. Wicket puts both ABOVE the 3rd party library in the header.

I want to render the code in a tag within the body to make sure that both have at least loaded before my code runs. The third party library has a DomReady event, but that at least requires the base variable to be defined.

I gather that I am meant to use FilteringHeaderResponse or HeaderResponseContainer somehow, but the examples/javadocs aren't very clear to me. Let's say my inline javascript was meant to be:

String js = "alert('Hello '" + userName + ");";

from java. It isn't, it's more complicated than that, but if it were, how would I, from a FormComponentPanel, render this inline javascript? And how can I dynamically include my custom js, but make sure it renders BELOW the 3rd party js in the header, but before my inline script runs?

Foi útil?

Solução

I ended up using:

response.render(OnDomReadyHeaderItem.forScript(writeGridJS()));

Outras dicas

Put your js into a file like my.js and make it a ResourceReference. Then add the libraries your code depends on as dependencies to this ResourceReference. here you got two good links on that:

http://wicketinaction.com/2012/07/wicket-6-resource-management/

http://wicket.apache.org/guide/guide/chapter14.html#chapter14_5

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top