Event Listening

From Documentation

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");
    }
}

Suggestions:

  • It is suggested to use this approach for component development, since it is subtle for application developers to notice its existence. In additions, it requires to extend the component class.

  1. An event attribute is an attribute starting with on

Listen by Use of an Event Listener

Composer and Event Listener Autowiring

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