Wire Components"

From Documentation
Line 31: Line 31:
 
= Usage Changed in 6.0.2=
 
= Usage Changed in 6.0.2=
  
The timing of <tt> BindComposer </tt> calling @Init method becomes earlier to fit more common situation. Hence, to wire components in a ViewModel, you should use <tt> @AfterCompose </tt> as follows:
+
The timing of <tt> BindComposer </tt> calling @Init method becomes earlier to handle more common situation. Hence, to wire components in a ViewModel, you should use <tt> @AfterCompose </tt> as follows:
  
  
Line 52: Line 52:
  
 
</source>
 
</source>
 
  
 
=Version History=
 
=Version History=

Revision as of 07:46, 10 July 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. However, we do not suggest this usage as it loses ViewModel an important advantage i.e. 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 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 what 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);
	}

}
  • Selectors.wireComponents()'s first parameters is Root View Component which can be retrieved by @ContextParam


Usage Changed in 6.0.2

The timing of BindComposer calling @Init method becomes earlier to handle more common situation. Hence, to wire components in a ViewModel, you should use @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);
	}

}

Version History

Last Update : 2012/07/10


Version Date Content
6.0.0 February 2012 The MVVM was introduced.




Last Update : 2012/07/10

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