MVVM Extension: Access UI Components Inside ViewModel"

From Documentation
Line 1: Line 1:
 
=MVVM Extension: Access UI Components Inside View Model =
 
=MVVM Extension: Access UI Components Inside View Model =
 +
 
Developing an application in MVVM pattern separates View and its backend clearly.  
 
Developing an application in MVVM pattern separates View and its backend clearly.  
 
Sometime developers want more control of components...
 
Sometime developers want more control of components...
 +
  
 
=Extend Case Scenario: Popup Detail=
 
=Extend Case Scenario: Popup Detail=
 +
 
In search example mentioned in previous article, users see an item's description in detail only when users click a item (as the below image shows). It's more convenient to show the description when the mouse hover an item. We'll implement this feature through MVVM with autowiring components.
 
In search example mentioned in previous article, users see an item's description in detail only when users click a item (as the below image shows). It's more convenient to show the description when the mouse hover an item. We'll implement this feature through MVVM with autowiring components.
  
 
[[File:smalltalks-mvvm-in-zk6-view-example.png]]
 
[[File:smalltalks-mvvm-in-zk6-view-example.png]]
  
 +
 +
=Add Popup=
 +
 +
Add a popup component to display item description at the end of window component.
 +
 +
<source lang="xml" >
 +
<popup id="msgPopup">
 +
<label id="msg"></label>
 +
</popup>
 +
</source>
 +
 +
 +
=Enable Autowire of View Model=
 +
 +
A View Model is a POJO basically, it doesn't have ability to wire UI components. We empower View Model to wire components with <tt>@init</tt>.
 
<source lang="java" >
 
<source lang="java" >
 
public class SearchAutowireVM{
 
public class SearchAutowireVM{
Line 27: Line 45:
 
}
 
}
 
</source>
 
</source>
 +
 +
We annotate a method <tt>public void init(BindContext) </tt> with <tt>@Init</tt>, hence the binder will invoke this method and pass in <tt>BindContext</tt> when it initializes the View Model. Inside the method, <tt>Selectors.wireVariables(component, this)</tt> will wire those variables with <tt>@Wire</tt>.
 +
 +
Remember to call <tt>Selectors.wireEventListeners(component, this)</tt>, if you want to register event handler with <tt>@Listen</tt>.

Revision as of 08:41, 7 December 2011

MVVM Extension: Access UI Components Inside View Model

Developing an application in MVVM pattern separates View and its backend clearly. Sometime developers want more control of components...


Extend Case Scenario: Popup Detail

In search example mentioned in previous article, users see an item's description in detail only when users click a item (as the below image shows). It's more convenient to show the description when the mouse hover an item. We'll implement this feature through MVVM with autowiring components.

Smalltalks-mvvm-in-zk6-view-example.png


Add Popup

Add a popup component to display item description at the end of window component.

	<popup id="msgPopup">
		<label id="msg"></label>
	</popup>


Enable Autowire of View Model

A View Model is a POJO basically, it doesn't have ability to wire UI components. We empower View Model to wire components with @init.

public class SearchAutowireVM{
        //other variables
	//UI component
	@Wire("#msgPopup")
	Popup popup;
	@Wire("#msg")
	Label msg;

	@Init
	public void init(BindContext ctx){
		//Returns associated root component of the binder
		Component component = ctx.getBinder().getView();
		Selectors.wireVariables(component, this);
//		Selectors.wireEventListeners(component, this);
	}
        //...
}

We annotate a method public void init(BindContext) with @Init, hence the binder will invoke this method and pass in BindContext when it initializes the View Model. Inside the method, Selectors.wireVariables(component, this) will wire those variables with @Wire.

Remember to call Selectors.wireEventListeners(component, this), if you want to register event handler with @Listen.