# ZK MVVM Modal Window

Senthilchettyin
2615 3 8
http://emrpms.blogspot.in...

Can someone please guide me how to return some value back to parent window when we call executions.create component using MVVM.

delete retag edit

## 8 Replies

Senthilchettyin
2615 3 8
http://emrpms.blogspot.in...

jimmyshiau
4921 5
http://www.zkoss.org/

Hi Senthilchettyin,

I have create a sample:

test1.zul

<zk>
<window id="win" apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('viewmodel.TestVM1')">
<button label="open window" onClick="@command('openWindow')"/>
</window>
</zk>


TestVM1.java

package viewmodel;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Window;

public class TestVM1 {
@Wire("#win")
private Window win;

@Init
public void init(@ContextParam(ContextType.VIEW) Component view) {
Selectors.wireComponents(view, this, false);
}

@Command
public void openWindow() {
Executions.createComponents("test2.zul", win, null);
}

@GlobalCommand
public void recieveData(@BindingParam("myData") String myData) {
System.out.println(myData);
}
}


test2.zul

<zk>
<window title="Win" border="normal" width="300px" mode="overlapped"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('viewmodel.TestVM2')">
<button label="send" onClick="@command('sendData') "/>
</window>
</zk>


TestVM2

 package viewmodel;

import java.util.HashMap;
import java.util.Map;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.Command;

public class TestVM2 {

@Command
public void sendData() {
Map args = new HashMap();
System.out.println("sendData");
BindUtils.postGlobalCommand(null, null, "recieveData", args);
}
}



is it you want to do?

for more detail, please refer to http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/MVVM/Data_Binding/Global_Command_Binding

Senthilchettyin
2615 3 8
http://emrpms.blogspot.in...

Thanks a lot. Yes this is want i am expecting.
One more help, how can i detach the test2 zul after posting global command. I remember, in MVC, we will use say win.detach().
But in mvvm ?

Senthilchettyin
2615 3 8
http://emrpms.blogspot.in...

Hi Jimmy

I found the solution from your example itself. I wired the window ID using selectcomposer and called win.detach;

And also, in order to help new comers, i have posted an example here
http://emrpms.blogspot.in/2012/06/mvvmlist-itemhibernatemysqlpart-2.html

jimmyshiau
4921 5
http://www.zkoss.org/

Look great ^ _ ^

simonmassey
90 1

I think that using @GlobalCommand for communication between ViewModels is a nicer way to do this. See this post http://stackoverflow.com/questions/11505371/how-to-get-a-viewmodel-from-another-viewmodel-in-zk-framework which is asking how a main window can pass the current selected item to a popup window. Clearly we can go back the other way by having the popup window use a @GlobalCommand to have a popup window pass a value back to the main window.

The reason why I prefer using @GlobalCommand in the zul rather than raising the global command in Java is the "no surprises" rule. With normal MVVM you can look at the ZUL file to know how the ViewModel is notified and you can look at the ViewModel to know how the business services are used. As soon as you use "BindUtils.postGlobalCommand(...);" inside a view model we have a "surprise" that an @Command also invokes a global command internally. If you move the posting of the global command back to an "@GlobalCommand" in the ZUL then you remove this "surprise" behaviour and you can easily see from the ZUL which components invoke local commands and which components invoke global commands.

Another way to look at it is that architecturally the Binder is part of the View framework which should be kept as separate as possible from your models. In terms of this presentation about patterns http://www.slideshare.net/simbo1905/design-patterns-in-zk-java-mvvm-as-modelviewbinder the ViewModel contains the business logic so should avoid calling the Binder which is part of the View.

dennis
3669 1 6
http://www.javaworld.com....

Hi, since zk 6.0.2, to wire component in a view model, you have to use new @AfterCompose annotation,

aktejo
155 3

This is more a further question,

As we know that global command provide a convenient way to communicate with many view model at once, also a bindutils.Postcommand provide dynamic mechanism.

I was wondering how could i invoke a command from clientside programmatically,...

[hide preview]