Scripts in ZUML"

From Documentation
Line 82: Line 82:
 
</blockquote>
 
</blockquote>
  
== Declare local variable ==
+
== 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.
+
If a variable is declared inside a pair of the curly braces, it is visible to the scope defined by the curly braces. It is called a local variable. For example,
  
You can see how <tt>{}</tt> and class name as <tt>Date</tt> affect scope and EL in the following example.
+
<source lang="xml">
 +
<zscript>
 +
void echo() {
 +
  String a_local_variable;
 +
}
 +
</script>
 +
</sourcE>
 +
 
 +
Here is another example,
  
 
<source lang="xml" >
 
<source lang="xml" >
 
<window>
 
<window>
 
<zscript>
 
<zscript>
 +
String first = "first";
 
{
 
{
 
    Date now = new Date();
 
    Date now = new Date();
Line 95: Line 104:
 
}
 
}
 
</zscript>
 
</zscript>
 +
0: ${first}
 
1:${abc}
 
1:${abc}
 
2:${now}
 
2:${now}
Line 100: Line 110:
 
</source>
 
</source>
  
The result shows: <tt>1:def 2: </tt> . <tt>abc</tt> is visible, and <tt>now</tt> is invisible.
+
The result shows: <tt>0:first 1:def 2: </tt> . It is because <tt>now</tt> is local variable and invisible to EL expressions. On the other hand, <tt>first</tt> and <tt>abc</tt> are both global variables that are visible to EL expressions. Also notice that <tt>abc</tt> is not declared but assigned directly, and it causes a global variable to be created.
  
 
Please refer to [http://www.beanshell.org/manual/bshmanual.pdf Beanshell's manual] and search "scoping"、"local" for more information.
 
Please refer to [http://www.beanshell.org/manual/bshmanual.pdf Beanshell's manual] and search "scoping"、"local" for more information.

Revision as of 09:54, 24 November 2010

Write Java Code in ZUML

For fast prototyping, you can embed codes in ZUML page. By default, it's Java and you could choose other languages, such as Groovy. Keep in mind, it's interpreted at run time (by Beanshell). Therefore you could define variables, methods, classes with it.


Notice that the performance of BeanShell is not good and, like any interpreter, typos can be found only when it is evaluated. Thus, it is suggest to limit the use of zscript in prototyping and quick-fix.

There are two ways to write zscript. First, you could the zscript code inside the zscript element:

<zscript>
//inside is zscript
//you can declare variable, function, and even Java class here.
void foo(String msg) {
    //...
}
comp.addEventListener("onClick",
    new EventListener() {
        public void onEvent(Event event) {
            //...
        }
    });
</zscript>

Second, you could put the zscript code inside event handler such that it will execute when the event is received.

<button onClick='alert("event handler for onXXX inside ZUML is also zscript")'/>

Notice that it is Java, interpreted at run time and running at the server. For client-side listening, please refer to the Client-side Event Listening section.

Distinguish zscript from EL

Keep in mind, EL is enclosed by ${}.

For example, ${self.label} is EL:

<window>	
	<button label="ok" id="${self.label}"/>
	${ok.label}		
</window>

While alert(self.label) is not EL, it's a zscript code:

<window>	
	<button label="ok" onClick='alert(self.label)'/>		
</window>

You cannot mix the use of EL with zscript:

<window>	
	<!-- It's wrong, for java don't accept syntax as ${}-->
	<button label="ok" onClick='alert(${self.label})'/>		
</window>

Also notice that the evaluation of EL expressions are very fast, while the execution of zscript code is slow. EL can be used in a production system, while zscript is suggested to use only in prototyping or quick-fix.

Java Interpreter

Scope for Each ID Space

Java interpreter (BeanShell) is a multi-scope interpreter. It creates a scope for each ID space. Since ID space is hierarchical, so is the scopes. 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, in the following example, two logical scopes are created for window[1] A and B, respectively. Therefore, var2 is visible only to window B, while var1 is visible to both window A and B.

<window id="A">
    <zscript>var1 = "abc";</zscript>
    <window id="B">
        <zscript>var2 = "def";</zscript>
    </window>
</window>

  1. Built in id space owner includes Window, Page and [[ZK Developer's Reference/UI Composing/Macro Component|macro components.

Declare Local Variable

If a variable is declared inside a pair of the curly braces, it is visible to the scope defined by the curly braces. It is called a local variable. For example,

<zscript>
void echo() {
   String a_local_variable;
}
</script>

Here is another example,

<window>
	<zscript>
	String first = "first";
	{
	    Date now = new Date();
	    abc ="def";
	}
	</zscript>
	0: ${first}
	1:${abc}
	2:${now}
</window>

The result shows: 0:first 1:def 2: . It is because now is local variable and invisible to EL expressions. On the other hand, first and abc are both global variables that are visible to EL expressions. Also notice that abc is not declared but assigned directly, and it causes a global variable to be created.

Please refer to 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 for more information.

Version History

Last Update : 2010/11/24


Version Date Content
     



Last Update : 2010/11/24

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