Event Listening"

From Documentation
Line 7: Line 7:
 
An event handler is a method specified as an event attribute of a ZK page or as a member of a component class.
 
An event handler is a method specified as an event attribute of a ZK page or as a member of a component class.
  
For example, we can declare an event handler by specifying an event attribute<ref>An event attribute is an attribute starting with <tt>on</tt></ref> as follows.
+
== Declare an Event Handler in a ZUL page ==
 +
An event handler can be declared in a ZUL page by specifying an event attribute<ref>An event attribute is an attribute starting with <tt>on</tt></ref>. For example,
  
 
<source lang="xml">
 
<source lang="xml">
Line 19: Line 20:
 
</source>
 
</source>
  
 +
Notice that the event handler declared in this way is interpreted at the run time, so it inherits all advantages and disadvantage of interpreter-based execution.
 +
 +
Advantange:
 +
 +
* It can be changed on the fly without recompiling and reloading the application.
 +
* Easy to maintain if the code snippet is small.
 +
 +
Disadvantage:
 +
 +
* Slower to run.
 +
* Compilation error can not be known in advance.
 +
* Hard to maintain if mixing business logic with user interface.
 +
 +
Suggestion:
 +
 +
* It is generally suggested to use this approach for 1) prototyping, or 2) simple event handling.
 +
 +
== Declare an Event Handler in Java ==
 
The other way to have an event listener is to declare it as a member of a component class. For example,
 
The other way to have an event listener is to declare it as a member of a component class. For example,
  

Revision as of 10:55, 23 August 2010

There are two ways to listen an event: an event handler and an event listener.

Listen by use of an Event Handler

An event handler is a method specified as an event attribute of a ZK page or as a member of a component class.

Declare an Event Handler in a ZUL page

An event handler can be declared in a ZUL page by specifying an event attribute[1]. For example,

<button label="hi" onClick='alert("Hello")'/>

where the content of the event handler is the code snippet in Java. The event handler will be interpreted at the run time (by use of BeanShell). If you prefer to use other language, you could specify the language name in front of it. For example, the following uses Groovy as the interpreter:

<button label="hi" onClick="groovy:alert('Hi, Groovy')"/>

Notice that the event handler declared in this way is interpreted at the run time, so it inherits all advantages and disadvantage of interpreter-based execution.

Advantange:

  • It can be changed on the fly without recompiling and reloading the application.
  • Easy to maintain if the code snippet is small.

Disadvantage:

  • Slower to run.
  • Compilation error can not be known in advance.
  • Hard to maintain if mixing business logic with user interface.

Suggestion:

  • It is generally suggested to use this approach for 1) prototyping, or 2) simple event handling.

Declare an Event Handler in Java

The other way to have an event listener is to declare it as a member of a component class. For example,

public class MyButton extends Button {
    public void onClick() {
        Messagebox.show("Hello");
    }
}

However, this approach is recommended if


  1. An event attribute is an attribute starting with on


Last Update : 2010/08/23

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