Objective
We'll build an "Add" function that would enable us to save new entries to the inventory.ZK Features in Action
- MVVM : Save, Form Binding, Conditional Binding
Add New Entries with MVVM Form Binding
we'll need to implement these parts:- Enhance our ViewModel POJO
- Add UI markup to present a form and decorate the markup with the appropriate annotations
public class InventoryVM { private List<item> items; private Item newItem; @NotifyChange("newItem") @Command public void createNewItem(){ newItem = new Item("", "",0, 0,new Date()); } @NotifyChange({"newItem","items"}) @Command public void saveItem() throws Exception{ DataService.getInstance().saveItem(newItem); newItem = null; items = getItems(); } @NotifyChange("newItem") @Command public void cancelSave() throws Exception{ newItem = null; } public List<item> getItems() throws Exception{ items = DataService.getInstance().getAllItems(); return items; } }
- Line 4, we declare an Item object named newItem which will reference the Item instance to be saved to the database.
- Line 6, @NotifyChange informs the binder to update the UI on the state of the associated ViewModel's property.
In our UI markup shown below, at line 8, we have a Groupbox annotated with visible="@load(not empty vm.newItem), hence the Groupbox will become visible once createNewItem assigns an instance of Item to newItem.
Simply put, @NotifyChange refreshes the UI with respect to the updates on ViewModel's properties. - Line 7, we annotate the createNewItem method with @Command and in our UI markup shown below, at line 4, we have a Toolbarbutton with onClick="@commnad(createNewItem)". So when the Toolbarbutton is clicked, the createNewItem method will be invoked.
- Similarly, from line 12 to 18, we have a saveItem method which is called when its corresponding onClick event is triggered. Once the new Item object is saved to the database cache, we reset the newItem to null and retrieve the new list of items. The changes made to the ViewModel properties newItem (now null again) and items (now with an extra entry) are reflected to the UI using @NotifyChange as before.
The Markup
<window apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('lab.sphota.zk.ctrl.InventoryVM')"> <toolbar> <toolbarbutton label="Add" onClick="@command('createNewItem')" /> </toolbar> <groupbox form="@id('itm') @load(vm.newItem) @save(vm.newItem, before='saveItem')" visible="@load(not empty vm.newItem)"> <caption label="New Item"></caption> <grid width="50%"> <rows> <row> <label value="Item Name" width="100px"></label> <textbox id="name" value="@bind(itm.name)" /> </row> <row> <label value="Model" width="100px"></label> <textbox value="@bind(itm.model)" /> </row> <row> <label value="Unit Price" width="100px"></label> <decimalbox value="@bind(itm.price)" format="#,###.00" constraint="no empty, no negative" /> </row> <row> <label value="Quantity" width="100px"></label> <spinner value="@bind(itm.qty)" constraint="no empty,min 0 max 999: Quantity Must be Greater Than Zero" /> </row> <row> <cell colspan="2" align="center"> <button width="80px" label="Save" onClick="@command('saveItem')" mold="trendy" /> <button width="80px" label="Cancel" onClick="@command('cancelSave')" mold="trendy" /> </cell> </row> </rows> </grid> </groupbox> <listbox> ... </listbox> </window>
- Line 1, we apply ZK's default implementation of its BindComposer. It is responsible for instantiating our ViewModel and Binder instances.
- Line 2, we supply the full class name of the ViewModel we wish to instantiate and give it an ID for future reference
- Line 4, we assign our ViewModel's "command method" createNewItem as the onClick event handler for the toolbar button.
- Line 6, the property newItem in ViewModel is made referenceable throughout the Groupbox using the ID "itm".
- Line 6,7, by using form binding, to avoid invalid or incomplete data saved to the ViewModel property, entries in the form are saved to a temporary object until the command method saveItem is called.
- Line 8, we show the Groupbox to enter a new Item entry only user has clicked the "Add" button; which in turn invokes createNewItem method and assigns the VM property newItem an instance of Item with default value(empty strings and 0s).
- Line 14, 18, 22, 27, we bind the Item properties with the input elements. @bind is effectively equivalent to @load plus @save.
In a Nuteshell
To sum up in point form:- Using form binding avoids directly modifying data in ViewModel properties by saving form entries to a temporary object. Data is written to the ViewModel properties only if the condition specified is satisfied; in our example, only if the saveItem method is invoked.
- @Command annotation allows the binder to map UI event handlers to ViewModel command methods.
- @NotifyChange informs the binder which ViewModel properties had been modified after the command method is executed so changes in data can then be reflected on the UI.
- We can assign values to any of UI components' attributes at run-time via MVVM binding to manipulate parameters such as visibility, style, disable/enable, etc.
In this post, we've not seen how data entries are validated. Before that, we'll implement the delete and edit functionalities in the next post.