Wire Components"
m |
m ((via JWB)) |
||
(17 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
{{ZKDevelopersReferencePageHeader}} | {{ZKDevelopersReferencePageHeader}} | ||
+ | {{Deprecated | url=[http://books.zkoss.org/zk-mvvm-book/8.0/advanced/wire_components.html zk-mvvm-book/8.0/data_binding/advanced/wire_components]|}} | ||
− | + | since 6.0.2 | |
− | One way is [[ZK Developer's Reference/MVVM/Advance/Parameters |passing components as parameters in command binding]] which we have talked before. Another is to | + | |
+ | Although the design principle of MVVM pattern is that '''ViewModel should not have any reference to UI components''', ZK still provides two ways to retrieve UI components in a ViewModel. However, we do not suggest this usage as it loses ViewModel an important advantage: loose coupling with View. '''Notice that binder also manipulates UI components, so your operation to UI components may affect binder's work. Please be careful when using it.''' | ||
+ | |||
+ | One way to get components is [[ZK Developer's Reference/MVVM/Advance/Parameters |passing components as parameters in command binding]] which we have talked before. Another is to call <code>Selectors.wireComponents()</code>. This way enables you to wire components with <code>@Wire</code> like what you do in a [[ZK Developer's Reference/MVC/Controller/Wire Components| SelectorComposer]]. You should call <code>Selectors.wireComponents()</code> in a method with <code>@AfterCompose</code> as follows: | ||
'''Example to wire components in a ViewModel''' | '''Example to wire components in a ViewModel''' | ||
− | <source lang="java" | + | <source lang="java" highlight="4,6,10,11"> |
public class SearchAutowireVM{ | public class SearchAutowireVM{ | ||
Line 17: | Line 21: | ||
Label msg; | Label msg; | ||
− | @ | + | @AfterCompose |
− | public void | + | public void afterCompose(@ContextParam(ContextType.VIEW) Component view){ |
Selectors.wireComponents(view, this, false); | Selectors.wireComponents(view, this, false); | ||
} | } | ||
Line 26: | Line 30: | ||
</source> | </source> | ||
− | *< | + | *<code>Selectors.wireComponents()</code>'s first parameters is Root View Component which can be retrieved by <code>@ContextParam</code> |
+ | |||
=Version History= | =Version History= | ||
− | + | ||
− | {| | + | {| class='wikitable' | width="100%" |
! Version !! Date !! Content | ! Version !! Date !! Content | ||
|- | |- | ||
Line 38: | Line 43: | ||
| February 2012 | | February 2012 | ||
| The MVVM was introduced. | | The MVVM was introduced. | ||
+ | |- | ||
+ | | 6.0.2 | ||
+ | | July 2012 | ||
+ | | The @AfterCompose was introduced. | ||
|} | |} | ||
Latest revision as of 07:35, 8 July 2022
This article is out of date, please refer to zk-mvvm-book/8.0/data_binding/advanced/wire_components for more up to date information.
since 6.0.2
Although the design principle of MVVM pattern is that ViewModel should not have any reference to UI components, ZK still provides two ways to retrieve UI components in a ViewModel. However, we do not suggest this usage as it loses ViewModel an important advantage: loose coupling with View. Notice that binder also manipulates UI components, so your operation to UI components may affect binder's work. Please be careful when using it.
One way to get components is passing components as parameters in command binding which we have talked before. Another is to call Selectors.wireComponents()
. This way enables you to wire components with @Wire
like what you do in a SelectorComposer. You should call Selectors.wireComponents()
in a method with @AfterCompose
as follows:
Example to wire components in a ViewModel
public class SearchAutowireVM{
//UI component
@Wire("#msgPopup")
Popup popup;
@Wire("#msg")
Label msg;
@AfterCompose
public void afterCompose(@ContextParam(ContextType.VIEW) Component view){
Selectors.wireComponents(view, this, false);
}
}
Selectors.wireComponents()
's first parameters is Root View Component which can be retrieved by@ContextParam
Version History
Version | Date | Content |
---|---|---|
6.0.0 | February 2012 | The MVVM was introduced. |
6.0.2 | July 2012 | The @AfterCompose was introduced. |