Binding in Special Attribute"

From Documentation
Line 10: Line 10:
 
= The "if" Versus the "visible" =
 
= The "if" Versus the "visible" =
  
Assume that an user's permission is unchanged when he visits the page and only the user with "admin" permission can see the "Delete" button. Then we can write a binding on <tt>if</tt> as follows:  
+
Assume that you want to show a "Delete" button only to a user who has administrative permission. We have several usage:  
  
<source lang='xml' high='3'>
+
'''specialAttribte.zul'''
 +
<source lang='xml' high='2, 4, 6'>
  
<!-- "if" usage  -->
+
<!-- wrong usage, no effect -->
<button label="Edit" />
+
<button label="Delete " if="@load(vm.currentUser.admin)" />
<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)" />
 +
<checkbox label="Is Admin" checked="@bind(vm.currentUser.admin)" />
 
</source>
 
</source>
  

Revision as of 03:44, 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)" />
		<checkbox label="Is Admin" checked="@bind(vm.currentUser.admin)" />

On the page above, those users without "admin" permission can't see the "Delete" button. Even if you assign the user with "admin" permission through a command, the "Delete" will not appear. But if you visit the page again after assigning the user "admin" permission, the button will appear.


  • if = ${vm.currentUser.admin}

The "forEach" Versus Children Binding

  • forEach -> children binding