CreateEvent"

From Documentation
Line 17: Line 17:
 
= Notes =
 
= Notes =
  
== Don't use with data binding (prior to 5.0.4) ==
+
== Use with data binding ==
 
 
Since 5.0.4, <tt>onCreate</tt> will be fired for each cloned component, so it is safe to use <tt>onCreate</tt> with the data binder
 
  
 
When the data binder processed a collection of data in, say, a grid or a listbox, it will detach the original one, and then clone it to represent each item of the data. For example,
 
When the data binder processed a collection of data in, say, a grid or a listbox, it will detach the original one, and then clone it to represent each item of the data. For example,
Line 34: Line 32:
 
# The data binder processes all annotations, after all components are created.
 
# The data binder processes all annotations, after all components are created.
 
## When handling <tt>each</tt>, the data binder detaches the listitem, invokes <javadoc method="clone()">org.zkoss.zk.ui.Component</javadoc> to make a clone for each item (person.interests), and attach the clone to the listbox.
 
## When handling <tt>each</tt>, the data binder detaches the listitem, invokes <javadoc method="clone()">org.zkoss.zk.ui.Component</javadoc> to make a clone for each item (person.interests), and attach the clone to the listbox.
# The listitem created by ZK Loader receives <tt>onCreate</tt>. Thus, whatever change the listener made won't affect the listitems attached the listbox (by the data binder).
+
# The listitem created by ZK Loader receives <tt>onCreate</tt>.
 +
 
 +
The detail behavior of step 3 is a bit different since 5.0.4. We discuss it more detailed in the following sections.
 +
 
 +
=== 5.0.3 and earlier ===
  
In summary, when using data binding, don't use <code>onCreate</code>.
+
With 5.0.3 and earlier, only the original listitem (the listitem used as template to be cloned) will receive <tt>onCreate</tt>.
 +
Thus, whatever change the listener made won't affect the cloned listitems.
 +
 
 +
In summary, when using data binding with 5.0.3 or earlier, don't use <code>onCreate</code>.
 +
 
 +
=== 5.0.4 ===
 +
Since 5.0.4, the data binder will fire <tt>onCreate</tt> to each cloned component, so it is safe to use <tt>onCreate</tt> with the data binder. However, there is one more thing to be careful: how the event listener is cloned when a component is cloned. By default, the cloned component will share the same listener with the other component. If it is not correct (for example, the listener might be an inner class that assumes <tt>this</tt> to be the initial component), the event listener shall implement <javadoc>ComponentCloneListener</javadoc> to clone the listener by itself.
 +
 
 +
<source lang="java">
 +
public FooCreateListener implements EventListener, ComponentCloneListener {
 +
  private Listitem _item;
 +
  public FooListener(Listitem item) {
 +
    _item = item;
 +
  }
 +
  public Object willClone(Component comp) {
 +
    return new FooListener((Listitem)comp);
 +
  }
 +
}
 +
</source>
  
 
=Supported events=
 
=Supported events=

Revision as of 08:55, 7 July 2010

CreateEvent

  • Demonstration: N/A
  • Java API: CreateEvent
  • JavaScript API: N/A

Employment/Purpose

Used to notify a window that all its children are created and initialized. UiEngine post this event to components that declares the onCreate handler (either as a method or as in instance definition).

Example

N/A

Notes

Use with data binding

When the data binder processed a collection of data in, say, a grid or a listbox, it will detach the original one, and then clone it to represent each item of the data. For example,

<listbox model="@{person.interests}">
	<listitem self="@{each=obj}" value="@{obj}" onCreate="foo()"/>
</listbox>

where the execution sequence is as follows.

  1. ZK Loader creates a listbox and a listitem, and posts onCreate to the listitem (since it has a listener).
  2. The data binder processes all annotations, after all components are created.
    1. When handling each, the data binder detaches the listitem, invokes Component.clone() to make a clone for each item (person.interests), and attach the clone to the listbox.
  3. The listitem created by ZK Loader receives onCreate.

The detail behavior of step 3 is a bit different since 5.0.4. We discuss it more detailed in the following sections.

5.0.3 and earlier

With 5.0.3 and earlier, only the original listitem (the listitem used as template to be cloned) will receive onCreate. Thus, whatever change the listener made won't affect the cloned listitems.

In summary, when using data binding with 5.0.3 or earlier, don't use onCreate.

5.0.4

Since 5.0.4, the data binder will fire onCreate to each cloned component, so it is safe to use onCreate with the data binder. However, there is one more thing to be careful: how the event listener is cloned when a component is cloned. By default, the cloned component will share the same listener with the other component. If it is not correct (for example, the listener might be an inner class that assumes this to be the initial component), the event listener shall implement ComponentCloneListener to clone the listener by itself.

public FooCreateListener implements EventListener, ComponentCloneListener {
  private Listitem _item;
  public FooListener(Listitem item) {
    _item = item;
  }
  public Object willClone(Component comp) {
    return new FooListener((Listitem)comp);
  }
}

Supported events

Name
Event Type
None None

Supported Children

*NONE

Use cases

Version Description Example Location
     

Version History

Version Date Content
     



Last Update : 2010/07/07

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