About Lance Lu

ZK in Action: 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.

ZK Feature in Action

  • MVVM : BindUtils

Implement Deletion with MVVM BindUtilsWe 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 in Action [2] : MVVM – Update View Programmatically from our JCG partner Lance Lu at the Tech Dojo blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


5 + = thirteen



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close