The use and apply Attribute

Embedding codes improperly in pages might cause maintenance headache. There are several ways to separate codes from views.

First, you could listen to events you care, and then invoke the proper methods accordingly. For example, you could invoke your methods to initialize, process and cancel upon the onCreate[10], onOK[11] and onCancel[12] events.

<window onCreate="MyManager.init(main)"onOK="MyManager.process(main)" onCancel="MyManager.cancel(main)"/>

In addition, you must have a Java class called MyManager shown as follows.

import org.zkoss.zul.Window;

public class MyManager {
    public static void init(Window main) { //does initialization    
    }    
    public static void save(Window main) { //saves the result    
    }    
    public static void cancel(Window main) { //cancel any changes    
    }    
}

However, the above approach requires you to embed some codes in the ZUML pages. The advantage of embedding codes in UI is easy to change the behavior dynamically (particularly in the prototype phase), but it still reveals some maintenance codes and the performance is bit slower[13].



[10] The onCreate event is sent when a window defined in a ZUML page is created.

[11] The onOK event is sent when user pressed the ENTER key.

[12] The onCancel event is sent when user pressed the ESC key.

[13] The codes specified in ZUML pages are interpreted a the run time by the Java interpreter.