Binding in Special Attribute"

From Documentation
Line 9: Line 9:
  
 
= The "if" Versus the "visible" =
 
= The "if" Versus the "visible" =
 +
<!-- binding in "visible" and "disabled" instead of "if" -->
  
 
Assume that you want to show a "Delete" button only to a user who has administrative permission. We have several usage:  
 
Assume that you want to show a "Delete" button only to a user who has administrative permission. We have several usage:  
  
 
'''specialAttribte.zul'''
 
'''specialAttribte.zul'''
<source lang='xml' high='2, 4, 6'>
+
<source lang='xml' high='2, 4, 6,7'>
  
 
<!-- wrong usage, no effect -->
 
<!-- wrong usage, no effect -->
Line 21: Line 22:
 
<!-- can change during user interaction -->
 
<!-- can change during user interaction -->
 
<button label="Delete (visible)" visible="@load(vm.currentUser.admin)" />
 
<button label="Delete (visible)" visible="@load(vm.currentUser.admin)" />
 +
<button label="Delete (disabled)" disabled="@load(not vm.currentUser.admin)" />
 
<checkbox label="Is Admin" checked="@bind(vm.currentUser.admin)" />
 
<checkbox label="Is Admin" checked="@bind(vm.currentUser.admin)" />
 
</source>
 
</source>
Line 26: Line 28:
 
The first one (line 2) is a wrong usage. The button will never be created.
 
The first one (line 2) is a wrong usage. The button will never be created.
  
<!-- binding in "visible" and "disable" instead of "if" -->
+
In the second one (line 4), the button's creation is determined when a user visits the page and won't appear unless the user becomes a administrator and visits the page again.
  
* if = ${vm.currentUser.admin}
+
The thrid one is what we recommend for most cases. You can binding on <tt>visible</tt>, and it brings you almost the same effect as <tt>if</tt>. The <tt>disabled</tt> also has similar effect.
  
 
= The "forEach" Versus Children Binding =
 
= The "forEach" Versus Children Binding =
 
* forEach -> children binding
 
* forEach -> children binding

Revision as of 04:09, 15 March 2013

location:

  • MVVM/Advanced/Parameter
  • MVVM/Advanced/Binding in Special Attributes

Special Attribute Issue

ZK Bind is a post-processing work on components after they are created and it can control most attributes to change a component's status. But there are some special attributes that ZK Bind can't work on them because those attributes' value are determined and fixed when components are created such as if and forEach. Therefore, binding on these special attributes takes no effect on components. But you may want the function that those special attributes provide, here we demonstrate alternives in MVVM approach.

The "if" Versus the "visible"

Assume that you want to show a "Delete" button only to a user who has administrative permission. We have several usage:

specialAttribte.zul

		<!-- wrong usage, no effect -->
		<button label="Delete " if="@load(vm.currentUser.admin)" />
		<!-- determined at the beginning -->
		<button label="Delete (EL)" if="${vm.currentUser.admin}" />
		<!-- can change during user interaction -->
		<button label="Delete (visible)" visible="@load(vm.currentUser.admin)" />
		<button label="Delete (disabled)" disabled="@load(not vm.currentUser.admin)" />
		<checkbox label="Is Admin" checked="@bind(vm.currentUser.admin)" />

The first one (line 2) is a wrong usage. The button will never be created.

In the second one (line 4), the button's creation is determined when a user visits the page and won't appear unless the user becomes a administrator and visits the page again.

The thrid one is what we recommend for most cases. You can binding on visible, and it brings you almost the same effect as if. The disabled also has similar effect.

The "forEach" Versus Children Binding

  • forEach -> children binding