Tuesday, July 31, 2012

ZK in Action [4] : Styling and Layout

In the previous ZK in Action posts we went through implementing a CRUD feature using ZK MVVM. We also quickly went through some styling code that may deserve more explanation.

In this post, we'll go over how to append new CSS styling rules onto ZK widgets and how to override the existing styling. We'll also introduce some basics of UI layout in ZK.

Objective

  • Use ZK's layout and container widgets to host the inventory CRUD feature we built in the previous posts.
  • Style the ZK widgets 

ZK Features in Action

  • Borderlayout
  • Hlayout
  • Tabbox
  • Include
  • sclass

Using Layouts and Containers

The Borderlayout and Hlayout

The Borderlayout divides the window into 5 sections as shown below:

Without further ado let's dissect the markup and see how it works:
<window ...">
 <borderlayout width="100%" height="100%">
  <north size="15%">
   <hlayout width="100%" height="100%">
   <label hflex="9" value="Alpha Dental" />
   <label hflex="1" value="Sign Out" ></label>
   </hlayout>
  </north>
  <east size="10%"></east>
  <center>
   <tabbox width="100%" height="100%" orient="vertical">
    <tabs width="15%">
     <tab label="Inventory" />
     <tab label="TBD" />
     <tab label="TBD"/>
    </tabs>
    <tabpanels>
     <tabpanel>
      <include src="inventory.zul"/>
     </tabpanel>
     <tabpanel></tabpanel>
     <tabpanel></tabpanel>
    </tabpanels>
   </tabbox>
  </center>
  <west size="10%" ></west>
  <south size="10%"></south>
 </borderlayout>
  • line 3 and 27, the north and south widgets can be adjusted for height but not width
  • line 9 and 26, the east and west widgets can be adjusted for width but not height
  • line 10, the center widget's dimensions are dependent on those entered for the north, west, south, and east widgets
  • from line 4 through 7, we wrap the two labels with an Hlayout so they'll be displayed side by side proportionally with respect to the "hflex" attribute we specified. That is, the Label assigned with hflex="9" has 9 times the  width of the Label assigned with hflex="1".
  • each inner widget (north, west, etc.) can accept only a single child component, hence, multiple widgets must be wrapped by a single container widget such as Hlayout before placed inside the Borderlayout inner widgets (north, west, etc.)
  • line 11, we place a Tabbox element and set its orientation as vertical in anticipation of embedding our inventory CRUD feature inside it
  • line 12 to 16, we put the heading for each tab
  • line 18, a Tabpanel is a container that holds a tab's content
  • line 19, we embed our inventory CRUD feature inside an Include tag. The widgets on inventory.zul will be attached to this page

Overriding the Existing ZK Styling Rules

The ZK default font properties and the background colours were modified so headings would be presented more prominently. Let's quickly explain how this is accomplished.
Using Chrome Developer Tool or the Firebug extension, we could easily inspect the source of our Borderlayout and find the ZK styling class for the ZK widgets as shown below:

From here we learned that the naming pattern for the highlighted region is z-north-body. Similarly, we could do the same for all the markup of interest and go ahead overriding their CSS styling rules:
<zk>
<style>
  .z-tab-ver .z-tab-ver-text { font-size: 18px; } 
  .z-north-body, .z-south-body { background:#A3D1F0 }
  .z-east-body, .z-west-body { background:#F8F9FB }
</style>
<window border="none" width="100%" height="100%">
 <borderlayout width="100%" height="100%">
  <north size="15%">...</north>
  <east size="10%"></east>
  <center>...</center>
  <west size="10%"></west>
  <south size="10%"></south>
 </borderlayout>
</window>
</zk>

Appending Additional Styling Rules via Style Attribute

Here we're modifying the styling of the Labels contained in the North widget. Since we only want these two Labels, not all of them, to be affected by our new styling, it does not make sense for us to override the original styling as we did before. For these isolated modifications, it suffice to simply assign the styling rules to the "style" attribute that comes with the ZK widgets:
<north size="15%">
   <hlayout width="100%" height="100%">
   <label value="Alpha Dental" style="font-size: 32px; font-style: italic; font-weight:bold; color:white; margin-left:8px;"/>
   <label value="Sign Out" style="font-size: 14px; font-weight:bold; color:grey; line-height:26px"></label>
   </hlayout>
  </north>...

Appending Additional Styling Rules via Sclass

An alternative to assign styling rules directly in the markup and pollute the code is to declare a styling class, abbreviated as "sclass", and assign the rules to the "sclass" attribute as shown here:
<zk>
<style>
  .company-heading {
   font-size: 32px; 
   font-style: italic; 
   font-weight:bold; 
   color:white; 
   margin-left:8px;
  }
</style>
<window ...>
 <borderlayout ...>
  <north ...> <label value="Alpha Dental" sclass="company-heading"/></north>
  ...
 </borderlayout>
</window>
</zk>

In a Nutshell

  • Three ways to modify the default ZK styling is covered in this post: override the existing ZK styling class, assign styling rules directly to a widget's style attribute, or define a CSS class in a CSS file or inside a Style tag then assign the class to the widget's sclass attribute
  • Use a developer tool(such as Firebug) to inspect the ZK widgets and find out which ZK style class to override
  • The hlex attribute allows developers to define widgets' width proportionally with respect to each other
  • Layout widgets help developers to divide the presentation window into sections

References

ZK Styling Guide
Borderlayout
Hlayout
Hflex

Wednesday, July 11, 2012

ZK in Action [3] : MVVM - Working Together with ZK Client API


In the previous posts we've implemented the following functionalities with ZK's MVVM:

A key distinction between ZK MVVM and ZK MVC implementation wise is that we do not access and manipulate the UI components directly in the controller(ViewModel) class. In this post, we'll see how we can delegate some of the UI manipulation to client side code, as well as how to pass parameters from View to ViewModel.

Objective

Build an update function to our simple inventory CRUD feature. Users can edit-in-place entries in the table and given the choice to update or discard the changes made. Modified entries are highlighted in red.



ZK Features in Action

  • ZK Client-side APIs
  • ZK Style Class
  • MVVM : Pass Parameters from View to ViewModel

Implementation in Steps

Enable In-place editing in Listbox so we can edit entries:

<listcell>
       <textbox inplace="true" value="@load(each.name)" ...</textbox>
   </listcell>
   ....
   <listcell>
       <doublebox inplace="true" value="@load(each.price)" ...</textbox>
   </listcell>
   ...
  • inplace="true" renders input elements such as Textbox without their borders displaying them as plain labels; the borders appear only if the input element is selected
  • line 2, 6,"each" refers to each Item object in the data collection

Once an entry is edited we want to give users the option to Update or Discard the change. 

The Update and Discard buttons need to be visible only if user has made modifications on the Listbox entries. First we define JavaScript functions to show and hide the Update and Discard buttons:
<toolbar>
    ...
    <span id="edit_btns" visible="false" ...>
        <toolbarbutton label="Update" .../>
        <toolbarbutton label="Discard" .../>
    </span>
</toolbar>

    <script type="text/javascript">
        function hideEditBtns(){
     jq('$edit_btns').hide();
        }
  
        function showEditBtns(){ 
     jq('$edit_btns').show();
        }

    </script>
    ...
  • line 2, we wrap the Update and Discard and set visibility to false
  • line 9, 13, we define functions which hide and show the Update and Discard buttons respectively
  • line 11, 15, we make use of jQuery selector jq( '$edit_btns') to retrieve the ZK widget whose ID is "edit_btns"; notice the selector pattern for a ZK widget ID is '$', not '#'

When entries in the Listbox are modified, we'll make the Update/Discard buttons visible and make the modified values red. Once either Update or Discard button is clicked, we'd like to hide the buttons again

Since this is pure UI interactions, we'll make use of ZK's client side APIs:
<style>
   .inputs { font-weight: 600; }
   .modified { color: red; }
</style>
...
    <toolbar xmlns:w="client" >
    ...
    <span id="edit_btns" visible="false" ...>
         <toolbarbutton label="Update" w:onClick="hideEditBtns()" .../>
         <toolbarbutton label="Discard" w:onClick="hideEditBtns()" .../>
    </span>
    </toolbar>

    <script type="text/javascript">
        //show hide functions

        zk.afterMount(function(){
            jq('.inputs').change(function(){
            showEditBtns();
            $(this).addClass('modified');
     })
        });
    </script>
    ...
    <listcell>
       <doublebox inplace="true" sclass="inputs" value="@load(each.price)" ...</textbox>
   </listcell>
   ...
  • line 2, we specify a style class for our input elements (Textbox, Intbox, Doublebox, Datebox) and assign it to input elements' sclass attribute, eg. line 26; sclass defines style class for ZK widgets
  • line 18~20, we get all the input elements by matching their sclass name and assign an onChange event handler. Once the value in an input element is changed, the Update/Discard buttons will become visible and the modified value will be highlighted in red.
  • line 17, zk.afterMount is run when ZK widgets are created
  • line 6, we specify the client namespace so we can register client side onClick event listeners with the syntax "w:onClick". Note we can still register our usual onClick event listener that's handled at the server simultaneously.
  • line 9, 10, we assign client side onClick event listener; the hideEditBtns function would be called to make the buttons invisible again

Define a method to store the modified Item objects into a collection so the changes could be updated in batch if user choose to do so:

public class InventoryVM {

    private HashSet<Item> itemsToUpdate = new HashSet<item>();
    ...

    @Command
    public void addToUpdate(@BindingParam("entry") Item item){
        itemsToUpdate.add(item);
    }
  • line 6, we annotate this method as a command method so it can be invoked from View
  • line 7, @BindingParam("entry") Item item binds an arbitrarily named parameter called "entry"; we anticipate the parameter would be of type Item

Create a method to update the changes made in View to the Data Model

public class InventoryVM {

    private List<Item> items;
    private HashSet<Item> itemsToUpdate = new HashSet<item>();
    ...

    @NotifyChange("items")
    @Command
    public void updateItems() throws Exception{
        for (Item i : itemsToUpdate){
            i.setDatemod(new Date());
            DataService.getInstance().updateItem(i);
        }
        itemsToUpdate.clear();
        items = getItems();
    }



When modifications are made on Listbox entries, invoke the addToUpdate method and pass to it the edited Item object which in turn is saved to the itemsToUpdate collection

<listitem>
 <listcell>
  <doublebox value="@load(each.price) 
                @save(each.name, before='updateItems')"  
                onChange="@command('addToUpdate',entry=each)" />
 </listcell>
 ...
</listitem>
  • @save(each.name, before='updateItems') ensures that modified values are not saved unless updateItems is called (ie. when user click the "Update" button)

Finally, when user clicks Update, we call the updateItems method to update changes to Data Model. If Discard is clicked, we call getItems to refresh the Listbox without applying any changes

...
 <toolbarbutton label="Update" onClick="@command('updateItems')" .../>
 <toolbarbutton label="Discard" onClick="@command('getItems')" .../>
 ...


In a Nutshell

  • Under the MVVM pattern we strive to keep the ViewModel code independent of any View components
  • Since we don't have direct reference to the UI components in the ViewModel code, we can delegate the UI manipulation (in our sample code, show/hide, style change) code to the client using ZK's client side APIs
  • We can make use of jQuery selectors and APIs at ZK client side
  • We can easily pass parameters from View to ViewModel with @BindingParam
Next up, we'll go over a bit more on ZK Styling before we tackle the MVVM validators and converters.

Reference

ViewModel (ZK in Action[0]~[3]):
public class InventoryVM {

 private List<Item> items;
 private Item newItem;
 private Item selected;
 private HashSet<Item> itemsToUpdate = new HashSet<Item>();
 
 public InventoryVM(){}
 
 //CREATE
 @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;
 }
 
 //READ
 @NotifyChange("items")
 @Command
 public List<Item> getItems() throws Exception{
  items = DataService.getInstance().getAllItems();
  for (Item j : items){
   System.out.println(j.getModel());
  }
  Clients.evalJavaScript("zk.afterMount(function(){jq('.inputs').removeClass('modified').change(function(){$(this).addClass('modified');showEditBtns();})});"); //how does afterMount work in this case?
  return items;
 }
 
 //UPDATE
 @NotifyChange("items")
 @Command
 public void updateItems() throws Exception{
  for (Item i : itemsToUpdate){
   i.setDatemod(new Date());
   DataService.getInstance().updateItem(i);
  }
  itemsToUpdate.clear();
  items = getItems();
 }
 
 @Command
 public void addToUpdate(@BindingParam("entry") Item item){
  itemsToUpdate.add(item);
 }
 
 //DELETE
 @Command
 public void deleteItem() throws Exception{
  if (selected != null){
  
   String str = "The item with name \""+selected.getName()+"\" and model \""+selected.getModel()+"\" will be deleted.";
   Messagebox.show(str,"Confirm Deletion", Messagebox.OK|Messagebox.CANCEL, Messagebox.QUESTION, 
    new EventListener<Event>(){
     @Override
     public void onEvent(Event event) throws Exception {
      if (event.getName().equals("onOK")){
       DataService.getInstance().deleteItem(selected);
       items = getItems();
       BindUtils.postNotifyChange(null, null, InventoryVM.this, "items");
      }
     }
   });
   
  } else {
   Messagebox.show("No Item was Selected");
  } 
 }
 

 public Item getNewItem() {
  return newItem;
 }

 public void setNewItem(Item newItem) {
  this.newItem = newItem;
 }

 public Item getselected() {
  return selected;
 }

 public void setselected(Item selected) {
  this.selected = selected;
 }
}
View (ZK in Action[0]~[3]):
<zk>
 <style>
  .z-toolbarbutton-cnt { font-size: 17px;} .edit-btns {border: 2px
  solid #7EAAC6; padding: 6px 4px 10px 4px; border-radius: 6px;}
  .inputs { font-weight: 600; } .modified { color: red; }
 </style>
 <script type="text/javascript">
  function hideEditBtns(){ jq('$edit_btns').hide(); }

  function showEditBtns(){ jq('$edit_btns').show(); }

  zk.afterMount(function(){ jq('.inputs').change(function(){
  $(this).addClass('modified'); showEditBtns(); }) });
 </script>
 <window apply="org.zkoss.bind.BindComposer"
  viewModel="@id('vm') @init('lab.sphota.zk.ctrl.InventoryVM')"
  xmlns:w="client">
  <toolbar width="100%">
   <toolbarbutton label="Add"
    onClick="@command('createNewItem')" />
   <toolbarbutton label="Delete"
    onClick="@command('deleteItem')"
    disabled="@load(empty vm.selected)" />
   <span id="edit_btns" sclass="edit-btns" visible="false">
    <toolbarbutton label="Update" 
     onClick="@command('updateItems')" w:onClick="hideEditBtns()"/>
    <toolbarbutton label="Discard"
     onClick="@command('getItems')" w:onClick="hideEditBtns()" />
   </span>
  </toolbar>
  <groupbox mold="3d"
   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 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" mold="trendy"
        onClick="@command('saveItem')"  />
       <button width="80px" label="Cancel" mold="trendy"
        onClick="@command('cancelSave')" />
      </cell>
     </row>
    </rows>
   </grid>
  </groupbox>
  <listbox selectedItem="@bind(vm.selected)" model="@load(vm.items) ">
   <listhead>
    <listheader label="Name" sort="auto" hflex="2" />
    <listheader label="Model" sort="auto" hflex="1" />
    <listheader label="Quantity" sort="auto" hflex="1" />
    <listheader label="Unit Price" sort="auto" hflex="1" />
    <listheader label="Last Modified" sort="auto" hflex="2" />
   </listhead>
   <template name="model">
    <listitem>
     <listcell>
      <textbox inplace="true" width="110px" sclass="inputs"
       value="@load(each.name) @save(each.name, before='updateItems')"
       onChange="@command('addToUpdate',entry=each)">
      </textbox>
     </listcell>
     <listcell>
      <textbox inplace="true" width="110px" sclass="inputs" 
       value="@load(each.model) @save(each.model, before='updateItems')"
       onChange="@command('addToUpdate',entry=each)" />
     </listcell>
     <listcell>
      <intbox inplace="true" sclass="inputs" 
       value="@load(each.qty) @save(each.qty, before='updateItems')"
       onChange="@command('addToUpdate',entry=each)" />
     </listcell>
     <listcell>
      <doublebox inplace="true" sclass="inputs" format="###,###.00" 
       value="@load(each.price) @save(each.price, before='updateItems')"
       onChange="@command('addToUpdate',entry=each)" />
     </listcell>
     <listcell label="@load(each.datemod)" />
    </listitem>
   </template>
  </listbox>
 </window>
</zk>

Monday, June 11, 2012

ZK in Action [2] : MVVM - Update View Programmatically

In the previous 2 posts we've used ZK's MVVM functionalities to:

We've seen when a method is decorated with the annotation @NotifyChange(), upon its execution completes, the Binder would be informed of the VM property's changes so that Binder can update the corresponding UI accordingly. 

In this post, whilst we implement the functionality of item deletion in our inventory, we'll see how we can update UI components programmatically at runtime.

Objective

Build a delete function to our simple inventory CRUD feature.

Select Item in Table, Click "Delete", Confirm Deletion


ZK Feature in Action


  • MVVM : BindUtils

Implement Deletion with MVVM BindUtils

We will:
  • Add markup for the delete button and assign it an onClick event handler
  • Implement command method "deleteItem()" in VM

The Markup
<window apply="org.zkoss.bind.BindComposer" 
 viewModel="@id('vm') @init('...InventoryVM')">
 <toolbar  width="100%">
  <toolbarbutton label="Add" onClick="@command('createNewItem')" />
  <toolbarbutton label="Delete" onClick="@command('deleteItem')" disabled="@load(empty vm.selected)"/>
 </toolbar>

  • line 5, assign a command method "deleteItem" to the delete button's onClick handler
  • line 5, "disabled="@(empty vm.selected)"" ensures that the delete button is functional only if an entry in the table has been selected



The ViewModel Class

public class InventoryVM {

    private Item selected;
    private List<Item> items;
    ...

    @Command
    public void deleteItem() throws Exception{
        if (selected != null){
            String str = "The item with name \""
                         +selected.getName()
                         +"\" and model \""
                         +selected.getModel()
                         +"\" will be deleted.";

        Messagebox.show(str,"Confirm Deletion", Messagebox.OK|Messagebox.CANCEL, Messagebox.QUESTION, 
            new EventListener<event>(){
                @Override
                public void onEvent(Event event) throws Exception {
                    if (event.getName().equals("onOK")){
                        DataService.getInstance().deleteItem(selected);
                        items = getItems();
                        BindUtils.postNotifyChange(null, null, InventoryVM.this, "items");
                    }); 
                } ...
    }
    ...
}

  • line 7, we decorate our deleteItem method with @Command so it can be wired as the onClick event handler in our added markup:
    <toolbarbutton label="Delete" onClick="@command('deleteItem')" />;
  • line 9, we go ahead with the deletion process only if an item is selected.
  • line 16, we show a Messagebox which prompts the user to confirm the deletion of the selected item.
  • line 20, if user clicks "OK", our program proceeds to delete the selected item.
  • line 23, we call BindUtils.postNotifyChange(String queueName, String queueScope, Object bean, String property) to update our inventory table. By giving the parameter queueScope a null value, the default desktop queue scope is used. The third and forth argument are given as such since we want to notify the Binder that property "items" in our InventoryVM instance has changed. The Binder would then update the UI (remove the item entry in the inventory table).

Wrap Up

The @NotifyChange annotation lets us update the UI through ZK MVVM's Binder to reflect changes made to the ViewModel properties. The notification is fired when the annotated method finishes executing. In our implementation, we attached an anonymous event listener class to a Messagebox. In this case, after deleteItem is executed, the annotation @NotifyChange("items") would falsely alert the Binder before the event handling is completed. A programmatic way to reflect state changes in ViewModel to the UI resolves this particular problem conveniently.

Next up, editing the entries with MVVM.

Reference

ZK Deverloper Reference

Thursday, May 31, 2012

ZK in Action [1] : MVVM - Form Binding

This is the second episode in our efforts to build a ZK application from the ground up. The previous post dealt with loading and rendering of data into a table using MVVM. In this post, we'll be introduced to ZK MVVM's form binding.

Objective

We'll build an "Add" function that would enable us to save new entries to the inventory.

A Form Appears When "Add" is Clicked


New Entry is Added When "Save" is Clicked


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
The ViewModel Class
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.

Reference

ZK Developer Reference

Wednesday, May 30, 2012

ZK in Action [0] : MVVM - Load and Render Data

A previous post had briefly introduced the RIA framework ZK and how its CSS Selector inspired controller mechanism alleviates some of the burdens that comes with UI changes by making the task of referencing UI components in the controller class a relatively flexible affair.

We then explored how the MVVM patterns in ZK allows a single ViewModel to serve different views in the last post.

This post marks the beginning of a series of posts that will go through steps in building a simple application from the ground up using ZK.

Objective

For now, we'll build a simple inventory management feature which is limited only to the loading and rendering of a data collection from a database into a table.

ZK Features in Action

  • MVVM : Load
  • Template Tag

Load and Render Data into a Table with MVVM

Assume there's a collection of objects named "Item" and there's a DataService class which takes care of caching and communication with the database (MongoDB and Morphia).
@Entity("items")
public class Item {
 @Id
 private ObjectId id;
 
 private String name;
 private String model;
 private int qty;
 private float price;
 private Date datemod;
 
        // getters & setters

To render data into a table as shown below in ZK, we'll need to implement these parts:
  • A POJO that will serve as our ViewModel
  • A ZK markup file as our presentation

The ViewModel Class
public class InventoryVM {

    private List<item> items;
 
    public List<item> getItems() throws Exception{
        items = DataService.getInstance().getAllItems();
        return items;
        }
    }

  • Line 3,  the list of items needs to be declared as a property of the VM class
  • Line 5, we need to provide a getter method so the Binder can retrieve the list of items. To recap, the Binder holds reference to the UI components and the ViewModel so it can keep data on both sides in sync as well as call command methods in ViewModel as events are triggered in View.

The Markup
<window apply="org.zkoss.bind.BindComposer" 
 viewModel="@id('vm') @init('lab.sphota.zk.ctrl.InventoryVM')">
 <listbox model="@load(vm.items) ">
  <listhead>
   <listheader label="Name" />
   <listheader label="Model" />
   <listheader label="Quantity" />
   <listheader label="Unit Price"/>
   <listheader label="Last Modified" />
  </listhead>
  <template name="model" var="item" >
   <listitem>
    <listcell>
     <textbox value="@load(item.name)" inplace="true" />
    </listcell>
    <listcell>
     <textbox value="@load(item.model)" inplace="true" />
    </listcell>
    <listcell>
     <spinner value="@load(item.qty)"  inplace="true" />
    </listcell>
    <listcell>
     <decimalbox value="@load(item.price)" inplace="true" 
     format="#,###.00"/>
    </listcell>
    <listcell label="@load(item.datemod)" />
   </listitem>
  </template>
 </listbox>
</window>

  • Line 1, we apply ZK's default implementation of its BindComposer. It is responsible for instantiating our VM instance as well as the Binder instance.
  • Line 2, we supply the full class name of the ViewModel we wish to instantiate and give it an ID (in this case, 'vm') for future reference
  • Line 3, we assign a data model, which we made as a property of our ViewModel instance, to the Listbox.
  • Line 11, we instruct the Template component to iterate through the given collection. We also declare a variable called "item" which will iteratively take on each Item object inside our collection. Alternatively, we can omit the variable declaration and use the keyword "each" to reference the data object (Item).
  • Line 14, 17, 20, 23, 26, we retrieve the Item properties which we'd like to be displayed in the Listbox.
  • Here we use input elements (Textbox, Spinner, Decimalbox) inside the Listcells in anticipation of our future implementation of an editable table. The attribute "inplace=true" will render these input elements as regular labels while they're not selected.

Wrap Up

ZK Binder is central to the workings of ZK MVVM. It holds references to both the UI components and the ViewModel. The ViewModel class is just a POJO where we declare and assign our data models. It exposes getter methods so Binder can retrieve and bind data to their respective annotated UI components. The template tag then allows us to iteratively render UI components with respect to the data model. In our case, a row of 5 Listcells with each cell holding a bean property is rendered iteratively through the bean collection using the template tag.

In the next post, we'll implement an "Add" feature so we can save new entries to our existing inventory using MVVM's form binding.

Reference

ZK Developer Reference

Monday, April 30, 2012

A First Look at MVVM in ZK 6

MVVM vs. MVC

In a previous post we've seen how the Ajax framework ZK adopts a CSS selector inspired Controller for wiring UI components in View and listening to their events. Under this ZK MVC pattern, the UI components in View need not to be bound to any Controller methods or data objects. The flexibility of using selector patterns as a mean to map View states and events to the Controller makes code more adaptive to change.

MVVM approaches separation of concern in a reverse direction. Under this pattern, a View-Model and a binder mechanism take place of the Controller. The binder maps requests from View to action logic in View-Model and updates any value (data) on both sides, allowing the View-Model to be independent of any particular View.

Anatomy of MVVM in ZK 6

The below is a schematic diagram of ZK 6's MVVM pattern:
Here are some additional points that's not conveyed in the diagram:
BindComposer:
  • implements ZK's standard controller interfaces (Composer & ComposerExt)
  • the default implementation is sufficient, no modifications necessary
View:
  • informs binder which method to call and what properties to update on the View-Model
View-Model:
  • just a POJO
  • communication with the binder is carried out via Java Annotation

MVVM in Action

Consider the task of displaying a simplified inventory without knowledge of the exact UI markup. An inventory is a collection of items, so we have the object representation of such:

public class Item {
 private String ID;
 private String name;
 private int quantity;
 private BigDecimal unitPrice;

        //getters & setters
}

It also makes sense to expect that an item on the list can be selected and operated on. Thus based on our knowledge and assumptions so far, we can go ahead and implement the View-Model.

public class InventoryVM {
 
    ListModelList<Item> inventory;
    Item selectedItem;
  
    public ListModelList<Item> getInventory(){
        inventory = new ListModelList<Item>(InventoryDAO.getInventory());
        return inventory;
    }

    public Item getSelectedItem() {
        return selectedItem;
    }
 
    public void setSelectedItem(Item selectedItem) {
        this.selectedItem = selectedItem;
    }

}   

Here we have a typical POJO for the View-Model implementation, data with their getters and setter.

View Implementation, "Take One"

Now suppose we later learned the requirements for the View is just a simple tabular display:

A possible mark-up to achieve the UI as indicated above is:
<window title="Inventory" border="normal" apply="org.zkoss.bind.BindComposer" 
 viewModel="@id('vm') @init('lab.zkoss.mvvm.ctrl.InventoryVM')" >
 <listbox model="@load(vm.inventory)" width="600px" >
  <auxhead><auxheader label="Inventory Summary" colspan="5" align="center"/> </auxhead>
  <listhead>
   <listheader width="15%" label="Item ID" sort="auto(ID)"/>
   <listheader width="20%" label="Name" sort="auto(name)"/>
   <listheader width="20%" label="Quantity" sort="auto(quantity)"/>
   <listheader width="20%" label="Unit Price" sort="auto(unitPrice)"/>
   <listheader width="25%" label="Net Value"/>
  </listhead>
  <template name="model" var="item">
   <listitem>
    <listcell><label value="@load(item.ID)"/></listcell>
    <listcell><label value="@load(item.name)"/></listcell>
    <listcell><label value="@load(item.quantity)"/></listcell>
    <listcell><label value="@load(item.unitPrice)"/></listcell>
    <listcell><label value="@load(item.unitPrice * item.quantity)"/></listcell>
   </listitem> 
  </template>
 </listbox>
</window>

Let's elaborate a bit on the mark-up here.

  • At line 1, we apply the default BindComposer to the Window component which makes all children components of the Window subject to the BindComposer's effect.
  • On the following line, we instruct the BindComposer which View-Model class to instantiate and we give the View-Model instance an ID so we could make reference to it.
  • Since we're loading a collection of data onto the Listbox, at line 3 we assign the property "inventory" of our View-Model instance, which is a collection of the Item objects, to Listbox's attribute "model". 
  • At line 12, we then make use of the model on our Template component. Template iterates its enclosed components according to the model it receives. In this case, we have 5 list items which makes up a row in the Listbox. 
  • In each Listcell, we load the properties of each object and display them in Labels.
Via ZK's binding system, we were able to access data in our View-Model instance and load them in View using annotations.

View Implementation, "Take Two"

Suppose later in development, it's agreed that the current tabular display takes too much space in our presentation and we're now asked to show the details of an item only when the item is selected in a Combobox, as shown below:


Though both the presentation and behaviour(detail is shown only upon user's selection) differ from our previous implementation, the View-Model class needs not be heavily modified. Since an item's detail will be rendered only when it is selected in the Combobox, it's obvious that we'd need to handle the "onSelect" event, let's add a new method doSelect:
public class InventoryVM {
 
    ListModelList<Item> inventory;
    Item selectedItem;

    @NotifyChange("selectedItem")
    @Command
    public void doSelect(){ }
    
    //getters & setters

}

A method annotated with @Command makes it eligible to be called from our mark-up by its name, in our case:

<combobox onSelect="@command('doSelect')" >

The annotation @NotifyChange("selectedItem") allows the property selectedItem to be updated automatically whenever user selects a new Item from the Combobox. For our purposes, no addition implementation is needed for the method doSelect. With this bit of change done, we can now see how this slightly-modified View-Model would work with our new mark-up:

<window title="Inventory" border="normal" apply="org.zkoss.bind.BindComposer" 
 viewModel="@id('vm') @init('lab.zkoss.mvvm.ctrl.InventoryVM')" width="600px">
 ...
  <combobox model="@load(vm.inventory)" 
     selectedItem="@bind(vm.selectedItem)" 
      onSelect="@command('doSelect')" >
   <template name="model" var="item">
    <comboitem label="@load(item.ID)"/>
   </template>
   <comboitem label="Test"/>
  </combobox>
  <listbox  visible="@load(not empty vm.selectedItem)" width="240px">
   <listhead>
    <listheader ></listheader>
    <listheader ></listheader>
   </listhead>
   <listitem>
    <listcell>
     <label value="Item Name: " />
    </listcell>
    <listcell>
     <label value="@load(vm.selectedItem.name)" />
    </listcell>
   </listitem>
   <listitem>
    <listcell>
     <label value="Unit Price: " />
    </listcell>
    <listcell>
     <label value="@load(vm.selectedItem.unitPrice)" />
    </listcell>
   </listitem>
   <listitem>
    <listcell>
     <label value="Units in Stock: " />
    </listcell>
    <listcell>
     <label value="@load(vm.selectedItem.quantity)" />
    </listcell>
   </listitem>
   <listitem>
    <listcell>
     <label value="Net Value: " />
    </listcell>
    <listcell>
     <label value="@load(vm.selectedItem.unitPrice * vm.selectedItem.quantity)" />
    </listcell>
   </listitem>
  </listbox>
 ...
</window>


  • At line 4, we load the data collection inventory to the Combobox's model attribute so it can iteratively display the ID for each Item object in the data model using the Template component declared on line 7. 
  • At line 5, the selectedItem attribute points to the most recently selected Item on that list of Item objects
  • At line 6, we've mapped the onSelect event to the View-Model's doSelect method
  • At line 12,  we make the Listbox containing an Item's detail visible only if the selectedItem property in View-Model is not empty (selectedItem will remain empty until an item is selected in the Combobox).
  • The selectedItem's properties are then loaded to fill out the Listbox.

Recap

Under the MVVM pattern, our View-Model class exposes its data and methods to the binder; there's no reference made to any particular View component. The View implementations access data or invoke event handlers via the binder.

In this post, we're only exposed to the fundamental workings of ZK's MVVM mechanisms. The binder is obviously not restricted to just loading data from the View-Model. In addition to saving data from View to ViewModel, we can also inject data converters and validators in the mix of View to View-Model communications. The MVVM pattern may also work in conjunction with the MVC model. That is, we can also wire components and listen to fired-events via the MVC Selector mechanism if we wish to do so.

We'll dig into some of these topics at a later time.

Reference

ZK Developer's Reference

Tuesday, March 27, 2012

JavaScript Closure Under the Hood

Closure is perhaps the most talked about feature in JavaScript. This post will attempt to deal with just enough of the theoretical aspects for us to appreciate the inner workings of the language behaviour we take granted for. The context of this discussion is  not specific to any JavaScript engine implementation.

Omni-Closures


In his book Secrets of the JavaScript Ninja, John Resig gave an informal description of closure as "A closure is a way to access and manipulate external variables from within a function."
Here is a rudimentary manifestation of closure that echoes the above description:

var y = 6;             // y is a global variable

function getSum(x){
    var z = 0;         // z is a local variable of  
    return x + y + z;  // getSum, whereas y is 
}                      // an external variable to getSum

getSum(4);             //returns 10

The variable y is not a local variable of getSum and yet it's been operated on within getSum. Closures are at work even for the most trivial code we'd write and take granted for.

A more explicit show of closure is when the local variables of an outer function remains to be referenced by the inner function even after the outer function's execution is complete:

function setOperators(y){       // outer function
    var z = 5;               
    function getSum(x){         //inner function
        return x + y + z;
    }
    return getSum;
}

var getSumRef = setOperators(4);//outer function's execution is complete and returns the function getSum
                                //
getSumRef(1);                   //returns 10

Function Scope?


Simply put, the scope for an entity Z refers to what other entities Z has access to. In JavaScript, a variable is said to have function scope. Instead of a rigorous but lengthy definition for it, let's consider this structure instead:

var i = 1;

function x(a){
    var j = 1;

    function y(){
        var k = 1;
    }
}

In the perspective of function x, var j and function y are accessible because they are declared within x, that is, they are local to x. Function x cannot access var k since k is within function y, hence local to y, but not to x. So far, functions x and y's behaviour adheres to that determined by their respective scopes.

However, by the merits of closure, functions can access variables outside of its local scope. Specifically, function y can access var j, and function x can access var i.

A closure is formed for function y which preserves the environment which y was created in, allowing y continue to have access to that environment. The variable j and argument a (argument for function x) are two of the constituents in this environment.

A Naive Visualization of Scope and Environment


Albeit neglecting some important details, sometimes a casual visualization can provide a mental picture that would eventually lead to a fuller understanding. So here is the attempt:

For function x:
  • local scope is the yellow area
  • environment is the blue area
  • x can access variables in yellow(local) and blue(environment), but not in green

For function y:
  • local scope is the green area
  • environment is the yellow area, plus the blue area
  • y can access variables in green(local), yellow(environment), and blue(environment)

Now we're better equipped to appreciate Wikipedia's definition of closure:
"A closure is a function together with a referencing environment for the non-local variables of that function."

Inner Workings of Closure


For ECMAScript, the proper name for this referencing environment 'e' is called an Activation Object when e is the local scope of a function, and a Global Object when e is the global scope. The activation object keeps local variables, named arguments, the arguments collection and this as key-value pairs.
Every time a function is being executed, an internal object called Execution Context is created for that specific execution. Execution context keeps a list of all activation objects and global object for the respective function being executed.

For instance, for our previous sample snippet:
function setOperators(y){       
    var z = 5;               
    function getSum(x){         
        return x + y + z;
    }
    return getSum;
}

var getSumRef = setOperators(4);  

getSumRef(1); //returns 10                  

The corresponding execution context would be:

When a closure is not needed for a function to reference its non-local variables, the function's execution context is discarded along with its activation objects. However, since the inner function getSum references non-local variable z, and argument y, a closure is created.

The closure makes a reference to the activation object for the "var getSumRef = setOperators(4)" execution context. Hence, even when setOperators' execution is completed, getSumRef remains to have access to the variables and the function getSum for evaluating the addition for us.

Digging Deeper(and Broader) for the Whole Picture


Here we've really only seen an expansion on the term "environment" in the context of a typical closure definition. To get a complete view of how scopes are managed and what function execution entails, refer to the following excellent resources:
High Performance JavaScript
http://jibbering.com/faq/notes/closures/
http://perfectionkills.com/understanding-delete/