0

How to include a modal confirmation message box before calling MVVM @command

asked 2012-06-12 18:39:57 +0800

davout gravatar image davout
1435 3 18

I've been playing around with the MVVM CRUD example.

In the delete use case I want to insert a message box style 'are you sure you want to delete (y/n)?' prompt before the delete command is actioned. Under ZK5 MVC I'd insert an 'alert' javascript entry into the onClick event handler. Is it possible to do something similar in ZK6 inside the ZUL and still call the @command('delete')?

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2012-06-13 03:06:48 +0800

MontyPan gravatar image MontyPan
435 3
http://xitop.blogspot.com...

How about this idea ?

// In ZUL
<button label="save" onClick="@command('click')" />

// In View Model
	@Command("click")
	public void click() {
		Messagebox.show(
			"Are you sure to save?", "Confirm Dialog",
			Messagebox.OK | Messagebox.IGNORE | Messagebox.CANCEL,
			Messagebox.QUESTION, 
			new org.zkoss.zk.ui.event.EventListener() {
				@Override
				public void onEvent(Event evt) throws InterruptedException {
					if (evt.getName().equals("onOK")) {
						System.out.println("Data Saved !");
					} else if (evt.getName().equals("onIgnore")) {
						Messagebox.show("Ignore Save", "Warning",
								Messagebox.OK, Messagebox.EXCLAMATION);
					} else {
						System.out.println("Save Canceled !");
					}
				}
			}
		);
	}

Thinking in MVVM, and let ZUL clean... :)

Regard,
Monty

link publish delete flag offensive edit

answered 2012-06-13 05:55:44 +0800

davout gravatar image davout
1435 3 18

Thanks... I understand how to complete the prompt inside the MVVM class.

The question is whether I can handle all the GUI inside the ZUL. Can I call a @command method inside some zscript?

link publish delete flag offensive edit

answered 2012-06-13 06:36:51 +0800

MontyPan gravatar image MontyPan
435 3
http://xitop.blogspot.com...

Can I call a @command method inside some zscript?

I think the answer is yes and no.

Yes, you can write View Model in zscript, or maybe exist other way to call @Command method in zscript.
(Notice : if you call method in View Model directly, it will not trigger @NotifyChange )
But in my opinion, it will disobey the spirit of MVVM pattern.
Let ZUL(View) is an absolute view, let View Model work on it's way.

Sorry to my impoliteness, but, please thinking in (pure) MVVM.


Regard,
Monty

link publish delete flag offensive edit

answered 2012-06-13 08:25:28 +0800

davout gravatar image davout
1435 3 18

I don't see how adding an 'alert' style popup in the ZUL file compromises the MVVM principle.

Think of it from a testing perspective. How can I build an automated test to verify a MVVM class @command method that performs a delete if inside the code it pops up a message box. Seems to me that button bindings need some form of validation /confirmation option that would be executed BEFORE the @command.

link publish delete flag offensive edit
link publish delete flag offensive edit

answered 2012-09-25 14:09:46 +0800

MonoMamerto gravatar image MonoMamerto
47 1

Take a look at this: http://books.zkoss.org/wiki/Small_Talks/2011/November/MVVM_in_ZK_6_-_Design_CRUD_page_by_MVVM_pattern

link publish delete flag offensive edit

answered 2012-12-06 04:34:46 +0800

tuneurt gravatar image tuneurt
3

updated 2012-12-06 04:54:47 +0800

(I'm using zk 6.5)

I've experienced the same problem with Confirm Box using MVVM pattern. Initially, I follow the solution in the link suggested by MonoMamerto which work well (as the MVVM style).

Doing such, I've to include the the same code (comfirm box) in each zul that I want to use Confirm Box. Moreover, if I want to change visual style of the Confirm Box, I've to edit every zul containing Confirm Box code.

As far as I know, the current version of zk (6.5?) does not provide @confirm (as requested by davout). So I created a MacroComponent to solve this limitation as workaround solution. This Component takes a String input as a message to display and evokes onYes/onNo event when users click Yes/No button.

You can use this component with MVVM pattern. To confirm delete, for example, instead of binding to 'delete()' method, you bind to 'confirm()' method which cause Confirm Box to appear. Then you bind 'onYes' event of Confirm Box to 'delete()' method. Here is an example.

<?component name="confirm" class="yourpackage.ConfirmBox" ?>
<zk>
...
    <confirm message="@load(vm.confirmMessage)" visible="@bind(vm.confirmDelete)" onYes="@command('delete')" ></confirm>
...
</zk>

My workaround solution might not be optimal, you can improve it or make it generic to support all type of MessageBox.

Here are sources for the mentioned MacroComponet I created.

confirmbox.zul

<zk>
    <window id="win" title="Confirmation" border="normal" mode="modal" 
            style="padding: 3px; min-width:350px; max-width: 450px; min-height: 100px;">
        <div style="padding: 5px;">
            <hlayout>
                <image src="~./zul/img/msgbox/question-btn.png" ></image>
                <separator ></separator>
                <label id="lblMessage" hflex="1" ></label>
            </hlayout>
            <separator style="margin: 5px;" ></separator>
            <div align="center">
                <button id="btnYes" width="70px" label="Yes" image="~./zul/img/misc/drag-allow.png" ></button>
                <button id="btnNo" width="70px" label="No" image="~./zul/img/misc/drag-disallow.png" ></button>
            </div>
        </div>
    </window>
</zk>

ConfirmBox.java

package yourpackage;

// import

@ComponentAnnotation({"message:@ZKBIND(ACCESS=both)", "visible:@ZKBIND(ACCESS=both)"})
public class ConfirmBox extends HtmlMacroComponent {
    
    private static final long serialVersionUID = 1L;

    private String message;

    @Wire("#win")
    private Window window;
    
    @Wire("#win #lblMessage")
    private Label lblMessage;
    
    @Wire("#win #btnYes")
    private Button btnYes;
    
    @Wire("#win #btnNo")
    private Button btnNo;

    public ConfirmBox() {
        setMacroURI("/WEB-INF/zul/component/confirmbox.zul");
    }
    
    @Override
    public void afterCompose() {
        super.afterCompose();
        if (window != null && isVisible()) {
            window.doModal();
        }
    }
    
    public String getValue() {
        return message;
    }
    
    @Listen("onClick=#win #btnYes")
    public void doYes() {
        Events.postEvent("onYes", this, null);
        setVisible(false);
    }
    
    @Listen("onClick=#win #btnNo")
    public void doNo() {
        setVisible(false);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
        if (lblMessage != null) {
            lblMessage.setValue(this.message);
        }
    }

    @Override
    public boolean isVisible() {
        return window.isVisible();
    }
    
    @Override
    public boolean setVisible(boolean visible) {
        if (window != null) {
            window.setVisible(visible);
            if (visible) {
                btnNo.focus();
                window.doModal();
            }
        }
        return visible;
    }
    
}


lang-addon.xml (place this file in e.g. WEB-INF)

<?xml version="1.0" encoding="UTF-8"?>
<language-addon>
    <addon-name>ciaaddon</addon-name>
    <language-name>xul/html</language-name>
    <component>
        <component-name>confirmBox</component-name>
        <component-class>yourpackage.ConfirmBox</component-class>
    </component>
</language-addon>


add this configuration to zk.xml (default WEB-INF/zk.xml)

<?xml version="1.0" encoding="UTF-8"?>
<zk>
...
    <language-config>
        <addon-uri>/WEB-INF/lang-addon.xml</addon-uri>        
    </language-config>
</zk>

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-06-12 18:39:57 +0800

Seen: 891 times

Last updated: Dec 06 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More