Wire Components"
From Documentation
m |
(fix error. should call Selectors.wireComponents()) |
||
Line 2: | Line 2: | ||
− | Although the | + | 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. '''We don't suggest this usage''' because it lose the ViewModel an important advantage: loose coupling with View. Please evaluate trade-off before using it. |
+ | |||
+ | 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 wire components by <tt> Selectors.wireComponents() </tt>. This way enables you to wire components with <tt> @Wire </tt> like you do in a <javadoc> org.zkoss.zk.ui.select.SelectorComposer</javadoc>. | ||
'''Example to wire components in a ViewModel''' | '''Example to wire components in a ViewModel''' | ||
− | <source lang="java" high="4,6,10,11"> | + | <source lang="java" high="4,6,9,10,11"> |
public class SearchAutowireVM{ | public class SearchAutowireVM{ | ||
Line 13: | Line 15: | ||
@Wire("#msg") | @Wire("#msg") | ||
Label msg; | Label msg; | ||
+ | |||
+ | @Init | ||
+ | public void init(@ContextParam(ContextType.VIEW) Component view){ | ||
+ | Selectors.wireComponents(view, this, false); | ||
+ | } | ||
} | } | ||
Line 18: | Line 25: | ||
</source> | </source> | ||
+ | *One of <tt> Selectors.wireComponents() </tt>'s parameters is Root View Component which can be retrieved by <tt> @ContextParam </tt> | ||
Revision as of 08:58, 28 May 2012
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. We don't suggest this usage because it lose the ViewModel an important advantage: loose coupling with View. Please evaluate trade-off before using it.
One way is passing components as parameters in command binding which we have talked before. Another is to wire components by Selectors.wireComponents() . This way enables you to wire components with @Wire like you do in a SelectorComposer.
Example to wire components in a ViewModel
public class SearchAutowireVM{
//UI component
@Wire("#msgPopup")
Popup popup;
@Wire("#msg")
Label msg;
@Init
public void init(@ContextParam(ContextType.VIEW) Component view){
Selectors.wireComponents(view, this, false);
}
}
- One of Selectors.wireComponents() 's 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. |