Scripts in ZUML"

From Documentation
Line 32: Line 32:
 
Keep in mind, EL is enclosed by ${}.  
 
Keep in mind, EL is enclosed by ${}.  
  
<tt>${self.label}</tt> is EL.
+
For example, <tt>${self.label}</tt> is EL:
 
<source lang="xml" >
 
<source lang="xml" >
 
<window>
 
<window>
Line 40: Line 40:
 
</source>
 
</source>
  
<tt>alert(self.label)</tt> is not EL, it's a piece of <tt>zscript</tt>
+
While <tt>alert(self.label)</tt> is not EL, it's a zscript code:
 
<source lang="xml" >
 
<source lang="xml" >
 
<window>
 
<window>
Line 47: Line 47:
 
</source>
 
</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>.
+
You cannot mix the use of EL with zscript:
 
 
 
<source lang="xml" >
 
<source lang="xml" >
 
<window>
 
<window>
Line 55: Line 54:
 
</window>
 
</window>
 
</source>
 
</source>
 +
 +
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 [[ZK Developer's Reference/Performance Tips/Use Compiled Java Codes|zscript is suggested to use only in prototyping or quick-fix]].
  
 
= Java Interpreter (BeanShell) =
 
= Java Interpreter (BeanShell) =

Revision as of 08:12, 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.

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 (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[1] A and B, respectively in the following example. Therefore, var2 is visible only to window B, while var1 is visible to both window A and B in the following example.

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

Notes

  1. Built in id space owner includes window, page and regular macro.

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 {} as follows.

You can see how {} and class name as Date affect scope and EL in the following example.

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

The result shows: 1:def 2: . abc is visible, and now is invisible.

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.