官網例子:http://books.zkoss.org/wiki/ZK_Getting_Started/Get_ZK_Up_and_Running_with_MVVM
(一)業務流程(與MVC例子一樣)
1、查詢小車列表
2、顯示某一小車詳細信息
(二) 工程
下載地址:http://www.cnblogs.com/jrsmith/admin/Files.aspx
searchMvvm.zul:
1、window: apply值從原來的某個SearchController,到現在的viewModel, org.zkoss.bind.BindComposer說明viewMoel是Binder,即將zul里的component綁定到SearchViewModel類。id('vm')唯一標識viewMoel
2、@bind(vm.xxxx): 即將某一個Component綁定到ViewModel具體的全局變量去,通過getter/setter方法交互。
注:個人認為這里的跟ZK MVC的原理一樣,只不過是將MVC里SearchController的變量映射放到View層而已。不過這樣的好處是你想映射哪個組件就映射哪個,不想映射就不加@bind。
3、@command('xxx'): 指定ViewModel里響應的方法,這里的是search()方法。
1 <window title="Search" width="600px" border="normal" 2 apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('com.core.vm.SearchViewModel')"> 3 <hbox align="center"> 4 Keyword: 5 <textbox value="@bind(vm.keyword)" /> 6 <button label="Search" image="/img/search.png" onClick="@command('search')" /> 7 </hbox> 8 <listbox height="160px" model="@bind(vm.carList)" emptyMessage="No car found in the result" 9 selectedItem="@bind(vm.selectedCar)"> 10 <listhead> 11 <listheader label="Model" /> 12 <listheader label="Make" /> 13 <listheader label="Price" width="20%"/> 14 </listhead> 15 <template name="model"> 16 <listitem> 17 <listcell label="@bind(each.model)"></listcell> 18 <listcell label="@bind(each.make)"></listcell> 19 <listcell>$<label value="@bind(each.price)" /></listcell> 20 </listitem> 21 </template> 22 </listbox> 23 <hbox style="margin-top:20px"> 24 <image width="250px" src="@bind(vm.selectedCar.preview)" /> 25 <vbox> 26 <label value="@bind(vm.selectedCar.model)" /> 27 <label value="@bind(vm.selectedCar.make)" /> 28 <label value="@bind(vm.selectedCar.price)" /> 29 <label value="@bind(vm.selectedCar.description)" /> 30 </vbox> 31 </hbox> 32 </window>
SearchViewModel.java:
1、這里純屬POJO,定義全局變量,getter/seter方法。變量名稱要與view層理的@bind(vm.xxx)名稱一致。
2、@command('xxx') : 對應view層里的響應事件,不指定名稱的話默認為修飾的方法名稱。
3、@NotifyChange("carList") : 指當carList內容改變后告訴ZK馬上更新view層里對應的carList Model.
1 package com.core.vm; 2 3 import java.util.List; 4 import org.zkoss.bind.annotation.*; 5 6 import com.core.model.Car; 7 import com.core.service.CarService; 8 import com.core.serviceImpl.CarServiceImpl; 9 10 public class SearchViewModel { 11 12 private String keyword; 13 private List<Car> carList; 14 private Car selectedCar; 15 16 private CarService carService = new CarServiceImpl(); 17 18 public void setKeyword(String keyword) { 19 this.keyword = keyword; 20 } 21 public String getKeyword() { 22 return keyword; 23 } 24 25 public List<Car> getCarList(){ 26 return carList; 27 } 28 29 30 public void setSelectedCar(Car selectedCar) { 31 this.selectedCar = selectedCar; 32 } 33 public Car getSelectedCar() { 34 return selectedCar; 35 } 36 37 38 @Command 39 @NotifyChange("carList") 40 public void search(){ 41 carList = carService.search(keyword); 42 } 43 }
(三)MVVM
左邊為MVC,右邊為MVVM。
兩者最大的區別在於MVVM的通過binder來實現數據的同步,而MVC則需要通過Controller來手動修改數據。
The biggest difference from the Controller in the MVC is that ViewModel should not contain any reference to UI components and knows nothing about the View's visual elements.(官網)
(四)運行