org.zkoss.zul
Class Script

java.lang.Object
  extended by org.zkoss.zk.ui.AbstractComponent
      extended by org.zkoss.zul.Script
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Component, Scope, ComponentCtrl, Script

public class Script
extends AbstractComponent
implements Script

A component to generate script codes that will be evaluated at the client. It is similar to HTML SCRIPT tag, except the defer option (setDefer(boolean)) will cause the evaluation of JavaScript until the widget has been instantiated and mounted to the DOM tree.

Note: it is the script codes running at the client, not at the server. Don't confuse it with the zscript element.

There are several way to embed script codes in a ZUML page:

Approach 1: Specify the URL of the JS file without defer. The JavaScript codes are evaluated as soon as the file is loaded.

<script src="my.js"/>
 

Approach 2: Specify the JavaScript codes directly without defer. The JavaScript codes are evaluated immediately before the widget is instantiated, so you cannot access any widget. Rather, it is used to do desktop-level initialization, such as defining a widget class, and a global function.

<script defer="true">
 zk.$package('foo');
 zk.load('zul.wgt', function () {
 foo.Foo = zk.$extends(zul.Widget, {
 //...
</script>
 

Approach 3: Specify the JavaScript codes directly with defer. The JavaScipt codes are evaluated after the widget is instantiated and mounted. Moreover, this references to the script widget, so you can access the widgets as follows.

<script defer="true">
 this.getFellow('l').setValue('new value');
 //...
</script>
 

Alternative to Approach 1, you can use the script directive as shown below..

<:?script src="/js/mine.js"?/>

Author:
tomyeh
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class org.zkoss.zk.ui.AbstractComponent
AbstractComponent.Children
 
Field Summary
 
Fields inherited from class org.zkoss.zk.ui.AbstractComponent
_visible
 
Fields inherited from interface org.zkoss.zk.ui.Component
APPLICATION_SCOPE, COMPONENT_SCOPE, DESKTOP_SCOPE, PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, SPACE_SCOPE
 
Fields inherited from interface org.zkoss.zk.ui.sys.ComponentCtrl
CE_BUSY_IGNORE, CE_DUPLICATE_IGNORE, CE_IMPORTANT, CE_NON_DEFERRABLE, CE_REPEAT_IGNORE
 
Constructor Summary
Script()
           
 
Method Summary
 java.lang.String getCharset()
          Returns the character enconding of the source.
 java.lang.String getContent()
          Returns the content of the script element.
 java.lang.String getPackages()
          Returns the list of packages to load before evaluating the script defined in getContent().
 java.lang.String getSrc()
          Returns the URI of the source that contains the script codes.
 java.lang.String getType()
          Deprecated. As of release 5.0.0, it is meaningless since text/javascript is always assumed.
protected  boolean isChildable()
          Not childable.
 boolean isDefer()
          Returns whether to defer the execution of the script codes until the widget is instantiated and mounted.
 void redraw(java.io.Writer out)
          Redraws this component and all its decendants.
protected  void renderProperties(ContentRenderer renderer)
          Called by (ComponentCtrl.redraw(java.io.Writer)) to render the properties, excluding the enclosing tag and children.
 void setCharset(java.lang.String charset)
          Sets the character encoding of the source.
 void setContent(java.lang.String content)
          Sets the content of the script element.
 void setDefer(boolean defer)
          Sets whether to defer the execution of the script codes.
 void setPackages(java.lang.String packages)
          Sets the list of packages to load before evaluating the script defined in getContent().
 void setSrc(java.lang.String src)
          Sets the URI of the source that contains the script codes.
 void setType(java.lang.String type)
          Deprecated. As of release 5.0.0, it is meaningless since text/javascript is always assumed.
 
Methods inherited from class org.zkoss.zk.ui.AbstractComponent
addAnnotation, addAnnotation, addClientEvent, addEventHandler, addEventListener, addForward, addForward, addForward, addForward, addMoved, addScopeListener, addSharedAnnotationMap, addSharedEventHandlerMap, appendChild, applyProperties, beforeChildAdded, beforeChildRemoved, beforeParentChanged, clone, containsVariable, detach, didActivate, didActivate, didDeserialize, didDeserialize, disableClientUpdate, equals, getAnnotatedProperties, getAnnotatedPropertiesBy, getAnnotation, getAnnotation, getAnnotations, getAnnotations, getAttribute, getAttribute, getAttribute, getAttributeOrFellow, getAttributes, getAttributes, getAuService, getChildren, getClientEvents, getDefinition, getDesktop, getEventHandler, getEventHandlerNames, getExtraCtrl, getFellow, getFellow, getFellowIfAny, getFellowIfAny, getFellows, getFirstChild, getId, getLastChild, getListenerIterator, getMold, getNamespace, getNextSibling, getPage, getParent, getPreviousSibling, getRoot, getSpaceOwner, getUuid, getVariable, getWidgetClass, getWidgetListener, getWidgetListenerNames, getWidgetOverride, getWidgetOverrideNames, hasAttribute, hasAttribute, hasAttribute, hasAttributeOrFellow, hasFellow, hasFellow, insertBefore, invalidate, isInvalidated, isListenerAvailable, isVisible, newChildren, newExtraCtrl, onChildAdded, onChildRemoved, onPageAttached, onPageDetached, onWrongValue, redrawChildren, removeAttribute, removeAttribute, removeAttribute, removeChild, removeEventListener, removeForward, removeForward, removeScopeListener, render, render, render, response, service, sessionDidActivate, sessionWillPassivate, setAttribute, setAttribute, setAttribute, setAuService, setDefinition, setDefinition, setId, setMold, setPage, setPageBefore, setParent, setVariable, setVisible, setWidgetListener, setWidgetOverride, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdateWidgetListener, smartUpdateWidgetOverride, toString, unsetVariable, updateByClient, willPassivate, willPassivate, willSerialize, willSerialize
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Script

public Script()
Method Detail

getType

public java.lang.String getType()
Deprecated. As of release 5.0.0, it is meaningless since text/javascript is always assumed.

Specified by:
getType in interface Script

setType

public void setType(java.lang.String type)
Deprecated. As of release 5.0.0, it is meaningless since text/javascript is always assumed.

Specified by:
setType in interface Script

getCharset

public java.lang.String getCharset()
Returns the character enconding of the source. It is used with getSrc().

Default: null.

Specified by:
getCharset in interface Script

setCharset

public void setCharset(java.lang.String charset)
Sets the character encoding of the source. It is used with setSrc(java.lang.String).

Specified by:
setCharset in interface Script

getSrc

public java.lang.String getSrc()
Returns the URI of the source that contains the script codes.

Default: null.

Specified by:
getSrc in interface Script

setSrc

public void setSrc(java.lang.String src)
Sets the URI of the source that contains the script codes.

You either add the script codes directly with the Label children, or set the URI to load the script codes with setSrc(java.lang.String). But, not both.

Specified by:
setSrc in interface Script
Parameters:
src - the URI of the source that contains the script codes

isDefer

public boolean isDefer()
Returns whether to defer the execution of the script codes until the widget is instantiated and mounted.

Default: false.

Specifying false (default), if you want to do the desktop-level (or class-level) initialization, such as defining a widget class or a global function.

Specifying true, if you want to access widgets. Notice that this references to this script widget.

Specified by:
isDefer in interface Script

setDefer

public void setDefer(boolean defer)
Sets whether to defer the execution of the script codes.

Specified by:
setDefer in interface Script
See Also:
isDefer()

getContent

public java.lang.String getContent()
Returns the content of the script element. By content we mean the JavaScript codes that will be enclosed by the HTML SCRIPT element.

Default: null.

Deriving class can override this method to return whatever it prefers (ingored if null).

Specified by:
getContent in interface Script
Since:
3.0.0

setContent

public void setContent(java.lang.String content)
Sets the content of the script element. By content we mean the JavaScript codes that will be enclosed by the HTML SCRIPT element.

Specified by:
setContent in interface Script
Since:
3.0.0

getPackages

public java.lang.String getPackages()
Returns the list of packages to load before evaluating the script defined in getContent(). It is meaning only if getContent() not null.

Default: null.

Since:
5.0.0

setPackages

public void setPackages(java.lang.String packages)
Sets the list of packages to load before evaluating the script defined in getContent(). If more than a package to load, separate them with comma.

Since:
5.0.0

redraw

public void redraw(java.io.Writer out)
            throws java.io.IOException
Description copied from class: AbstractComponent
Redraws this component and all its decendants.

Default: It uses JsContentRenderer to render all information in JavaScript codes. For devices that don't support JavaScript, it must override this method. To generate all information, it first invokes AbstractComponent.renderProperties(org.zkoss.zk.ui.sys.ContentRenderer) to render component's properties, and then AbstractComponent.redrawChildren(java.io.Writer) to redraw children (and descendants) (by calling their AbstractComponent.redraw(java.io.Writer)).

If a dervied class wants to render more properties, it can override AbstractComponent.renderProperties(org.zkoss.zk.ui.sys.ContentRenderer).

If a derived class renders only a subset of its children (such as paging/cropping), it could override AbstractComponent.redrawChildren(java.io.Writer).

If a deriving class wants to do something before AbstractComponent.renderProperties(org.zkoss.zk.ui.sys.ContentRenderer), it has to override AbstractComponent.redraw(java.io.Writer).

If a deriving class doesn't want to render in JavaScript codes, it has to override AbstractComponent.redraw(java.io.Writer) with the proper implementation of ContentRenderer.

Specified by:
redraw in interface ComponentCtrl
Overrides:
redraw in class AbstractComponent
Throws:
java.io.IOException

renderProperties

protected void renderProperties(ContentRenderer renderer)
                         throws java.io.IOException
Description copied from class: AbstractComponent
Called by (ComponentCtrl.redraw(java.io.Writer)) to render the properties, excluding the enclosing tag and children.

Default: it renders AbstractComponent.getId() if it was assigned, and event names if listened (and listed in AbstractComponent.getClientEvents()).

Note: it doesn't render AbstractComponent.getWidgetClass(), AbstractComponent.getUuid() and AbstractComponent.getMold(), which are caller's job.

Overrides:
renderProperties in class AbstractComponent
Throws:
java.io.IOException

isChildable

protected boolean isChildable()
Not childable.

Overrides:
isChildable in class AbstractComponent


Copyright © 2005-2009 Potix Corporation. All Rights Reserved. SourceForge.net Logo