Didn't think of that approach, looks good. But to clear up my previous
suggestion since I guess I wasn't clear enough and its useful in other
situations also, you need to _override_ getAssociatedMarkupStream(boolean), not
just call it.
public static final String JAVASCRIPT_PLACEHOLDER = "%JS_HERE%";
public MarkupStream getAssociatedMarkupStream(boolean throwEx) {
String jsToInsert = ... // create your java script
String html = ... // read HTML file via getClass().getResource
html = html.replace(JAVASCRIPT_PLACEHOLDER, jsToInsert);
IResourceStream srs = new StringResourceStream(html);
MarkupResourceStream mrs = new MarkupResourceStream(srs);
Markup markup = new SimpleMarkupLoader().loadMarkup(this, mrs, null, true);
return new MarkupStream(markup);
}
and your panel
<wicket:panel>
<!-- some markup here -->
%JS_HERE%
<!-- more markup -->
</wicket:panel>
Craig
_____
From: Anton Veretennikov [mailto:[email protected]]
To: [email protected]
Sent: Sat, 10 Oct 2009 11:24:12 -0400
Subject: Re: how to inject arbitrary javascript code to a component markup?
For example I use this markup:
<script wicket:id="focusScript"></script>
and associate it with simple class:
public class FocusScript extends Label {
public FocusScript(String id, String focusFieldMarkupId, boolean selectAll)
{
super(id, getFocusScript(focusFieldMarkupId, selectAll));
setEscapeModelStrings(false);
}
@Override
protected void onComponentTag(ComponentTag tag) {
tag.put("language", "javascript");
tag.put("type", "text/javascript");
super.onComponentTag(tag);
}
@Override
protected void onComponentTagBody(MarkupStream markupStream,
ComponentTag openTag) {
super.onComponentTagBody(markupStream, openTag);
checkComponentTag(openTag, "script");
}
public static String getFocusScript(String focusFieldMarkupId,
boolean selectAll) {
return "document.getElementById('" + focusFieldMarkupId + "').focus();" +
(selectAll?("document.getElementById('" +
focusFieldMarkupId + "').select()"):"");
}
}
On Sat, Oct 10, 2009 at 11:11 PM, Igor Vaynberg <[email protected]>
wrote:
> can you not just use a label whose model is the "<script>...</script>"
string?
>
> -igor
>
> On Sat, Oct 10, 2009 at 5:34 AM, Paul Huang <[email protected]> wrote:
>> Suppose I write my own wicket component called XYZ that have the following
>> markup
>>
>> <wicket:panel>
>> <div wicket:id="id_xxx">
>> ....
>> </div>
>> </wicket:panel>
>>
>> How can I inject some js code into this markup so when it's rendered in a
>> page, I got something like
>>
>> <wicket:panel>
>> <div wicket:id="id_xxx">
>> <script type="text/javascript">
>> var data=["abc", "efg"];
>> document.write(data[0]);
>> </script>
>> .....
>> <div>
>> <wicket:panel>
>>
>> You may ask why I dont simply keep the js code into the component markup
>> "XYZ.html", this is because the value of "data" variable will be provided
by
>> the user and is not fixed.
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]