Pass Arguments to Include Component"
(8 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. | + | |
+ | 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=" | + | <source lang="xml" high="5"> |
− | < | + | <div apply="org.zkoss.bind.BindComposer" |
− | + | viewModel="@id('vm') @init('org.zkoss.reference.developer.mvvm.advance.InnerVM')"> | |
<groupbox width="400px"> | <groupbox width="400px"> | ||
− | Argument passed from | + | Argument passed from outer page: |
− | <label value="@load( | + | <label value="@load(vm.typeFromOuter)"/> |
− | |||
− | |||
</groupbox> | </groupbox> | ||
− | </ | + | </div> |
+ | |||
</source> | </source> | ||
− | + | ||
== Arguments from ViewModel == | == 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 | + | 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. |
<source lang="xml"> | <source lang="xml"> | ||
− | <include type="@load(vm.myArgument)" src="@load( | + | <include type="@load(vm.myArgument)" src="@load('inner.zul')"/> |
</source> | </source> | ||
− | * | + | * 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= | =Version History= |
Revision as of 07:28, 28 May 2015
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
Version | Date | Content |
---|---|---|
6.0.0 | February 2012 | The MVVM was introduced. |