Pass Arguments to Include Component"

From Documentation
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
 +
{{Deprecated | url=[http://books.zkoss.org/zk-mvvm-book/8.0/advanced/pass_arguments_to_include_component.html zk-mvvm-book/8.0/data_binding/advanced/pass_arguments_to_include_component]|}}
  
When you load a ZUL page using <tt> Executions.createComponents("mypage.zul", args) </tt> or  <tt> <include> </tt> and pass arguments. ZK bind annotation EL expression can not reference those arguments directly because of life cycle issue. What a binder does is a post-processing action after component creation. At the moment of post-processing, it cannot obtain arguments' value. The simplest solution is to add an custom attribute to hold arguments for later reference. Let's see an example.
+
 
 +
When you load a ZUL page using <tt> Executions.createComponents("mypage.zul", args) </tt> or  <tt> <include> </tt> and pass arguments. ZK bind annotation EL expression can not reference those arguments directly because of life cycle issue. What a binder does is a post-processing action after component creation. At the moment of post-processing, it cannot obtain arguments' value. The simplest solution is to add an custom attribute to hold arguments for later reference or use <tt>@ExecutionArgParam</tt> to retrieve in a ViewModel's initial method. Let's see an example.
  
 
'''outer.zul'''
 
'''outer.zul'''
Line 11: Line 13:
 
</source>
 
</source>
 
* Here we pass an argument named "type" to an included ZUL.
 
* Here we pass an argument named "type" to an included ZUL.
 +
 +
 +
'''ViewModel for included zul'''
 +
<source lang="java" high="6">
 +
 +
public class InnerVM {
 +
 +
private String typeFromOuter;
 +
 +
@Init
 +
public void init(@ExecutionArgParam("type") String type){
 +
typeFromOuter = type;
 +
}
 +
...
 +
}
 +
</source>
 +
* Line 6: Retrieve the passed argument with key "type".
 +
  
 
'''inner.zul'''
 
'''inner.zul'''
  
<source lang="xml" high="2,5,7">
+
<source lang="xml" high="5">
<zk>
+
<div apply="org.zkoss.bind.BindComposer"
<custom-attributes type="${arg.type}" />
+
viewModel="@id('vm') @init('org.zkoss.reference.developer.mvvm.advance.InnerVM')">
 
<groupbox width="400px">
 
<groupbox width="400px">
Argument passed from outter page:  
+
Argument passed from outer page:
<label value="@load(empty type?'Argument Not Available':type)"
+
<label value="@load(vm.typeFromOuter)"/>
style="@load(empty type?'color:red':'')"/>
 
<button label="MyCommand" onClick="@command('mycommand', mytype=type)" />
 
 
</groupbox>
 
</groupbox>
</zk>
+
</div>
 +
 
 
</source>
 
</source>
* We should use a custom attribute (line 2) to hold the argument for later use (line 5,7).
+
 
  
  

Revision as of 07:28, 28 May 2015


DocumentationZK Developer's ReferenceMVVMAdvancedPass Arguments to Include Component
Pass Arguments to Include Component


Stop.png This article is out of date, please refer to zk-mvvm-book/8.0/data_binding/advanced/pass_arguments_to_include_component for more up to date information.


When you load a ZUL page using Executions.createComponents("mypage.zul", args) or <include> and pass arguments. ZK bind annotation EL expression can not reference those arguments directly because of life cycle issue. What a binder does is a post-processing action after component creation. At the moment of post-processing, it cannot obtain arguments' value. The simplest solution is to add an custom attribute to hold arguments for later reference or use @ExecutionArgParam to retrieve in a ViewModel's initial method. Let's see an example.

outer.zul

<include type="outerPageLiteralValue" src="inner.zul" />
  • Here we pass an argument named "type" to an included ZUL.


ViewModel for included zul

public class InnerVM {

	private String typeFromOuter;
	
	@Init
	public void init(@ExecutionArgParam("type") String type){
		typeFromOuter = type;
	}
	...
}
  • Line 6: Retrieve the passed argument with key "type".


inner.zul

<div apply="org.zkoss.bind.BindComposer"
		viewModel="@id('vm') @init('org.zkoss.reference.developer.mvvm.advance.InnerVM')">
	<groupbox width="400px">
		Argument passed from outer page:
		<label value="@load(vm.typeFromOuter)"/>
	</groupbox>
</div>


Arguments from ViewModel

If an argument comes from a ViewModel's property, "src" attribute must also load with data binding and be specified at last attribute for life cycle issue. The reason is similar to previous case, if you specify "src" attribute with a static value, the included page's components are created first but at that moment the argument's value is not determined. Unless both arguments and "src" attribute bound with ViewModel with data binding, they can be processed in the same phase. Then creating included page can access arguments correctly. In addition, ZK only resolves arguments's EL once at component creation, so ViewModel's property change in the future won't reflect on included components.

<include type="@load(vm.myArgument)" src="@load('inner.zul')"/>
  • The "src" attribute must be specified at last attribute.
  • ZK only resolves arguments's EL once at component creation, so ViewModel's property change in the future won't reflect on included components.

Version History

Last Update : 2015/05/28


Version Date Content
6.0.0 February 2012 The MVVM was introduced.




Last Update : 2015/05/28

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