zscript

From Documentation
Revision as of 07:20, 12 July 2010 by Maya001122 (talk | contribs)

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


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 zscript. Please refer to Write Java code in your ZUML for a simple example. Please also refer to Zscript, java, EL for some useful examples about zscript.

There are two ways to write zscript. First, inside <zscript></zscript>:

<zscript>
//inside is zscript
//you can declare variable, function, and even Java class here.
</zscript>

Second, inside event handler.

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

Hints to read zscript

When beginner first meet zscript, he usually get confused with "where is the declaration of such variable?". ZK use Beanshell as java interpreter. As 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, zscript 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 zscript includes initialization and declaring global variables and methods.

Distinguish zscript from EL

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

${self.label} is EL.

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

alert(self.label) is not EL, it's a piece of zscript

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

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 zscript.

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

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.

See Also


Quiz

Finish the code with zscript, that when you click the button, total price will be calculated and showed.

<window>	
	Price : <intbox id="price"/>
	Count : <intbox id="count"/>
	Total Price : <intbox id="total"/>
	<button label="Total Price"/>
</window>


Last Update : 2010/07/12

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