zscript"

From Documentation
m (Replaced content with '{{ZKDevelopersGuidePageHeader}} This section outlines zscript, what it is and how it is used. {{ZKDevelopersGuideHeadingToc}} {{ ZKDevelopersGuidePageFooter}}')
Line 1: Line 1:
 
{{ZKDevelopersGuidePageHeader}}
 
{{ZKDevelopersGuidePageHeader}}
== Write Java code in ZUML ==
 
For fast prototyping, you can embed codes in ZUML page. By default, it's java. Keep in mind, it's simply java but interpreted by Beanshell. Therefore you can define variables, methods, event class inside <tt>zscript</tt>. Please refer to [[Getting_started#Write_java_code_in_you_ZUML | Write Java code in your ZUML]] for a simple example. Please also refer to [[Zscript%2C_java%2C_EL | Zscript, java, EL]] for some useful examples about zscript.
 
  
There are two ways to write <tt>zscript</tt>. First, inside <zscript></zscript>:
+
This section outlines zscript, what it is and how it is used.
<source lang="xml" >
 
<zscript>
 
//inside is zscript
 
//you can declare variable, function, and even Java class here.
 
</zscript>
 
</source>
 
  
Second, inside event handler.
+
{{ZKDevelopersGuideHeadingToc}}
<source lang="xml" >
 
<button onClick='alert("event handler for onXXX inside ZUML is also zscript")'/>
 
</source>
 
  
==Hints to read <tt>zscript</tt>==
 
When beginner first meet <tt>zscript</tt>, he usually get confused with "where is the declaration of such variable?". ZK use [http://www.beanshell.org/ Beanshell] as java interpreter. As [http://www.beanshell.org/docs.html Beanshell's manual] says: '''Beanshell supports "loose" or dynamically typed variable. That is, you can refer to variables without declaring them first and without specifying any type.''' Also, <tt>zscript</tt> can access component's field and implicit object, they may look like strange undeclared variables to java programmer.
 
 
Some packages are imported implicitly by Beanshell, but some package have to be imported by developer.
 
 
Typical usage of <tt>zscript</tt> includes initialization and declaring global variables and methods.
 
 
==Distinguish <tt>zscript</tt> from EL==
 
Keep in mind, EL is enclosed by ${}.
 
 
<tt>${self.label}</tt> is EL.
 
<source lang="xml" >
 
<window>
 
<button label="ok" id="${self.label}"/>
 
${ok.label}
 
</window>
 
</source>
 
 
<tt>alert(self.label)</tt> is not EL, it's a piece of <tt>zscript</tt>
 
<source lang="xml" >
 
<window>
 
<button label="ok" onClick='alert(self.label)'/>
 
</window>
 
</source>
 
 
Although they both look alike, but EL and zscript have different life cycle, EL is not available at certain phase. Therefore in some cases, you can't use EL but <tt>zscript</tt>.
 
 
<source lang="xml" >
 
<window>
 
<!-- It's wrong, for java don't accept syntax as ${}-->
 
<button label="ok" onClick='alert(${self.label})'/>
 
</window>
 
</source>
 
 
== Java Interpreter (BeanShell) ==
 
=== Scope for Each ID space ===
 
Java interpreter (BeanShell) is a typical multi-scope interpreter. It creates an interpreter-dependent scope for each ID space. And it's '''hierarchical'''. If a variable can't be found in current id space, it will go further to parent's id space try to resolve the variable. For example, two logical scopes are created for window<ref>Built in id space owner includes <tt>window</tt>, <tt>page</tt> and <tt>regular macro</tt>.</ref> <tt>A</tt> and <tt>B</tt>, respectively in the following example. Therefore, <tt>var2</tt> is visible only to window <tt>B</tt>, while <tt>var1</tt> is visible to both window <tt>A</tt> and <tt>B </tt>in the following example.
 
 
<source lang="xml" >
 
<window id="A">
 
    <zscript>var1 = "abc";</zscript>
 
    <window id="B">
 
        <zscript>var2 = "def";</zscript>
 
    </window>
 
</window>
 
</source>
 
 
'''Notes'''
 
<references/>
 
 
=== Declare local variable ===
 
In additions, you shall use local variables if possible. A local variable is declared with the class name, and it is visible only to a particular scope of zscript codes. Furthermore, you can make a local variable invisible to EL expressions by enclosing it with <tt>{}</tt> as follows.
 
 
You can see how <tt>{}</tt> and class name as <tt>Date</tt> affect scope and EL in the following example.
 
 
<source lang="xml" >
 
<window>
 
<zscript>
 
{
 
    Date now = new Date();
 
    abc ="def";
 
}
 
</zscript>
 
1:${abc}
 
2:${now}
 
</window>
 
</source>
 
 
The result shows: <tt>1:def 2: </tt> . <tt>abc</tt> is visible, and <tt>now</tt> is invisible.
 
 
Please refer to [http://www.beanshell.org/manual/bshmanual.pdf Beanshell's manual] and search "scoping"、"local" for more information.
 
 
== zscript other than Java ==
 
Currently, zscript already support java, javascript, ruby, groovy. You can add other language to support zscript as you like. Please refer to section [[Zscript_other_than_java | Zscript other than java]] for more information.
 
 
== See Also ==
 
*[[ZUML_ZK_Elements#The_zscript_Element | The zscript Element ]]
 
*[[Zscript%2C_java%2C_EL | Zscript, java, EL]] : useful tricks about zscript
 
*[http://www.zkoss.org/doc/devref-single/index.html#id4858599 The variable-resolver Directive] : Specifies the variable resolver that will be used by the zscript interpreter to resolve unknown variables.
 
 
 
== Quiz ==
 
Finish the code with <tt>zscript</tt>, that when you click the button, total price will be calculated and showed.
 
 
<source lang="xml" >
 
<window>
 
Price : <intbox id="price"/>
 
Count : <intbox id="count"/>
 
Total Price : <intbox id="total"/>
 
<button label="Total Price"/>
 
</window>
 
</source>
 
 
{{ ZKDevelopersGuidePageFooter}}
 
{{ ZKDevelopersGuidePageFooter}}

Revision as of 02:25, 1 September 2010

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


This section outlines zscript, what it is and how it is used.





Last Update : 2010/09/01

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.