|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.click.element.Element org.apache.click.element.ResourceElement org.apache.click.element.JsScript
public class JsScript
Provides a HEAD element for including inline JavaScript using the <script> tag.
Example usage:public class MyPage extends Page { public List getHeadElements() { // We use lazy loading to ensure the JS is only added the // first time this method is called. if (headElements == null) { // Get the head elements from the super implementation headElements = super.getHeadElements(); JsScript jsScript = new JsScript("alert('Hello World!);"); headElements.add(jsScript); } return headElements; } }The jsScript instance will be rendered as follows:
<script type="text/javascript"> alert('Hello World'); </script>Below is an example showing how to render inline Javascript from a Velocity template. First we create a Velocity template (/js/mycorp-template.js) which contains the variable $divId that must be replaced at runtime with the real Div ID attribute:
hide = function() { var div = document.getElementById('$divId'); div.style.display = "none"; }Next is the Page implementation:
public class MyPage extends Page { public List getHeadElements() { // We use lazy loading to ensure the JS is only added the // first time this method is called. if (headElements == null) { // Get the head elements from the super implementation headElements = super.getHeadElements(); // Create a default template model to pass to the template Map model = ClickUtils.createTemplateModel(this, getContext()); // Add the id of the div to hide model.put("divId", "myDiv"); // Specify the path to the JavaScript template String templatePath = "/js/mycorp-template.js"; // Create the inline JavaScript for the given template path and model JsScript jsScript = new JsScript(templatePath, model); headElements.add(jsScript); } return headElements; } }The jsScript instance will render as follows (assuming the context path is myApp):
<script type="text/javascript"> hide = function() { var div = document.getElementById('myDiv'); div.style.display = "none"; } </style>
JavaScript
in CDATA tags. Two use cases are common for doing this:
setCharacterData(boolean)
to true. Below is shown how the JavaScript
content would be rendered:
<script type="text/javascript"> /∗<![CDATA[∗/ if(x < y) alert('Hello'); /∗]]>∗/ </script>Notice the CDATA tags are commented out which ensures older browsers that don't understand the CDATA tag, will ignore it and only process the actual content. For an overview of XHTML validation and CDATA tags please see http://javascript.about.com/library/blxhtml.htm.
Field Summary |
---|
Fields inherited from class org.apache.click.element.ResourceElement |
---|
IF_IE, IF_IE7, IF_LESS_THAN_IE7, IF_LESS_THAN_IE9, IF_LESS_THAN_OR_EQUAL_TO_IE7 |
Constructor Summary | |
---|---|
JsScript()
Construct a new inline JavaScript element. |
|
JsScript(String content)
Construct a new inline JavaScript element with the given content. |
|
JsScript(String template,
Map<String,Object> model)
Construct a new inline JavaScript element for the given template path and template model. |
Method Summary | |
---|---|
boolean |
equals(Object o)
|
String |
getContent()
Return the JsScript content. |
Map<String,Object> |
getModel()
Return the model of the template
to render. |
String |
getTag()
Returns the JavaScript HTML tag: <script>. |
String |
getTemplate()
Return the path of the template to render. |
int |
hashCode()
|
boolean |
isCharacterData()
Return true if the JsScript's content should be wrapped in CDATA tags, false otherwise. |
boolean |
isExecuteOnDomReady()
Return true if the JsScript content must be executed as soon as the browser DOM is ready, false otherwise. |
void |
render(HtmlStringBuffer buffer)
Render the HTML representation of the JsScript element to the specified buffer. |
protected void |
renderContent(HtmlStringBuffer buffer,
Context context)
Render the JsScript content
to the specified buffer. |
protected void |
renderDomReadyPrefix(HtmlStringBuffer buffer)
Render the "Click.addLoadEvent" function prefix to ensure the script is executed as soon as the browser DOM is available. |
protected void |
renderDomReadySuffix(HtmlStringBuffer buffer)
Render the "Click.addLoadEvent" function suffix. |
void |
setCharacterData(boolean characterData)
Sets whether the JsScript's content should be wrapped in CDATA tags or not. |
void |
setContent(String content)
Set the JsScript content. |
void |
setExecuteOnDomReady(boolean executeOnDomReady)
Sets whether the JsScript content must be executed as soon as the browser DOM is ready. |
void |
setModel(Map<String,Object> model)
Set the model of the template to render. |
void |
setTemplate(String template)
Set the path of the template to render. |
Methods inherited from class org.apache.click.element.ResourceElement |
---|
getConditionalComment, getVersionIndicator, isRenderId, isUnique, setConditionalComment, setRenderId, setVersionIndicator |
Methods inherited from class org.apache.click.element.Element |
---|
appendAttributes, getAttribute, getAttributes, getContext, getId, hasAttribute, hasAttributes, setAttribute, setId, toString |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public JsScript()
public JsScript(String content)
content
- the JavaScript contentpublic JsScript(String template, Map<String,Object> model)
content
.
For example:
public class MyPage extends Page { public void onInit() { Context context = getContext(); // Create a default template model Map model = ClickUtils.createTemplateModel(this, context); // Create JsScript for the given template path and model JsScript script = new JsScript("/mypage-template.js", model); // Add script to the Page Head elements getHeadElements().add(script); } }
template
- the path of the template to rendermodel
- the template modelMethod Detail |
---|
public String getTag()
getTag
in class Element
public String getContent()
public void setContent(String content)
content
- the JsScript contentpublic boolean isCharacterData()
public void setCharacterData(boolean characterData)
characterData
- true indicates that the JsScript's content should be
wrapped in CDATA tags, false otherwisepublic boolean isExecuteOnDomReady()
setExecuteOnDomReady(boolean)
public void setExecuteOnDomReady(boolean executeOnDomReady)
Ajax
requests the JsScript content won't be registered with the
"Click.addLoadEvent" function because Ajax requests does not trigger
the browser's DOM loaded event. Instead the JsScript content will be
evaluated immediately by the browser.
executeOnDomReady
- indicates whether the JsScript content must be
executed as soon as the browser DOM is ready.public String getTemplate()
setTemplate(java.lang.String)
public void setTemplate(String template)
template
property is set, the template and model
will be merged and the result will be rendered together with any JsScript
content
.
template
- the path of the template to renderpublic Map<String,Object> getModel()
template
to render.
setModel(java.util.Map)
public void setModel(Map<String,Object> model)
template
property is set, the template and model
will be merged and the result will be rendered together with any JsScript
content
.
model
- the model of the template to renderpublic void render(HtmlStringBuffer buffer)
render
in class ResourceElement
buffer
- the buffer to render output topublic boolean equals(Object o)
equals
in class Object
o
- the object with which to compare this instance with
Object.equals(java.lang.Object)
public int hashCode()
hashCode
in class Object
Object.hashCode()
protected void renderContent(HtmlStringBuffer buffer, Context context)
content
to the specified buffer.
Please note: if the template
property is set, this method will merge the template
and model
and the result will be
rendered, together with the JsScript
content
,
to the specified buffer.
buffer
- the buffer to append the output tocontext
- the request contextprotected void renderDomReadyPrefix(HtmlStringBuffer buffer)
buffer
- the buffer to append the Click.addLoadEvent function torenderDomReadySuffix(org.apache.click.util.HtmlStringBuffer)
protected void renderDomReadySuffix(HtmlStringBuffer buffer)
buffer
- buffer to append the conditional comment prefixrenderDomReadyPrefix(org.apache.click.util.HtmlStringBuffer)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |