Client-side Event Listening"

From Documentation
Line 59: Line 59:
  
 
Notice that it is Java and running at the server.
 
Notice that it is Java and running at the server.
 +
 +
Also notice that EL expressions are not allowed (i.e., not interpreted) if you assign it directly. It is because EL expressions are interpreted by ZK Loader when loading a ZUL page. However, it is easy to construct a string to any content you want with Java.
  
 
= Register a Client-side Listener in Client-Side JavaScript =
 
= Register a Client-side Listener in Client-Side JavaScript =

Revision as of 11:57, 26 August 2010


Client-side Event Listening

Overview

ZK allows applications to handle events at both server and client side. Handling events at the server side, as described in the previous sections, are more common, since the listeners can access the backend services directly. However, handling event at the client side improves the responsiveness. For example, it is better to be done with a client-side listener if you want to open the drop-down list when a comobox gains the focus.

The rule of thumb is to use server-side listeners first since it is easier, and then improve the responsiveness of the critical part, if any, with the client-side listener.

Declare a Client-side Listener in a ZUL Page

Declaring a client-side listener in a ZUL page is similar to declaring a server-side listener, except

  1. Use the client namespace, http://www.zkoss.org/2005/zk/client (aka., client)
  2. It is JavaScript
  3. Use this to reference to the target widget (while the event is referenced with event)

For example,

<combobox xmlns:w="client" w:onFocus="this.open()"/>

Notice that EL expressions are allowed in the JavaScript code (for the client-side listener). Thus, it is straightforward to embed the server-side data to the client-side listener. For example,

<window id="wnd" title="main">
<combobox xmlns:w="client" w:onFocus="zk.log('${wnd.title}')"/>
</window>

If you want to escape it, place a backslash between $ and {, such as w:onFocus="zk.log('$\{wnd.title}')".

The Relationship between Client and Server-side Event Listener

It is allowed to register both the client and server-side event listeners. They will be both invoked. Of course, the client-side listener is called first, and then the server-side listener. For example,

<div>
  <combobox xmlns:w="client" w:onFocus="this.open()"
   onFocus='self.parent.appendChild(new Label("focus"))'/>
</div>

If you want to stop the event propagation such that the server won't receive the event, you could invoke Event.stop(Map). For example, the server-side listener won't be invoked in the following example:

<div>
  <combobox xmlns:w="client" w:onFocus="this.open(); event.stop();"
   onFocus='self.parent.appendChild(new Label("focus"))'/>
</div>

Declare a Client-side Listener in Java

The other way to declare a client-side listener at the server is Component.setWidgetListener(String, String). For example,

combobox.setWidgetListener("onFocus", "this.open()");

Notice that it is Java and running at the server.

Also notice that EL expressions are not allowed (i.e., not interpreted) if you assign it directly. It is because EL expressions are interpreted by ZK Loader when loading a ZUL page. However, it is easy to construct a string to any content you want with Java.

Register a Client-side Listener in Client-Side JavaScript

Version History

Version Date Content
     



Last Update : 2010/08/26

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