McIlwee, Craig wrote:
>
> 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
>
Carig- Just tried your approach, it works as well. Thanks for your help.
Here is the complete code, just in case someone else may need it.
> @Override
> public MarkupStream getAssociatedMarkupStream(boolean throwEx) {
> String jsToInsert ="my js code"; // create your java script
> InputStream
> htmlStream=this.getClass().getResourceAsStream("YOUR_MARKUP_TEMPLATE.html");
> String html=convertStreamToString(htmlStream);
> html = html.replace("JAVASCRIPT_PLACEHOLDER", jsToInsert);
> IResourceStream srs = new StringResourceStream(html);
> MarkupResourceStream mrs = new MarkupResourceStream(srs);
> Markup markup = null;
> try {
> markup = new
> SimpleMarkupLoader().loadMarkup(this, mrs, null, true);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (ResourceStreamNotFoundException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> return new MarkupStream(markup);
> }
>
> public String convertStreamToString(InputStream is) {
> /*
> * To convert the InputStream to String we use the
> BufferedReader.readLine()
> * method. We iterate until the BufferedReader return
> null which
> means
> * there's no more data to read. Each line will
> appended to a
> StringBuilder
> * and returned as String.
> */
> BufferedReader reader = new BufferedReader(new
> InputStreamReader(is));
> StringBuilder sb = new StringBuilder();
>
> String line = null;
> try {
> while ((line = reader.readLine()) != null) {
> sb.append(line + "\n");
> }
> } catch (IOException e) {
> e.printStackTrace();
> } finally {
> try {
> is.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> return sb.toString();
> }
>
--
View this message in context:
http://www.nabble.com/how-to-inject-arbitrary-javascript-code-to-a-component-markup--tp25833726p25837543.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org