1

ZK MVVM Modal Window

asked 2012-06-14 00:46:13 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 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 flag offensive retag edit

8 Replies

Sort by ยป oldest newest

answered 2012-06-15 02:11:01 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Can some help please.............................

link publish delete flag offensive edit

answered 2012-06-15 08:59:16 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

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();
		args.put("myData", "please check");
		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

link publish delete flag offensive edit

answered 2012-06-18 04:52:42 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 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 ?

link publish delete flag offensive edit

answered 2012-06-18 07:39:38 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 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

link publish delete flag offensive edit

answered 2012-06-19 09:21:30 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Look great ^ _ ^
Thanks for your share.

link publish delete flag offensive edit

answered 2012-07-24 05:36:43 +0800

simonmassey gravatar image simonmassey
90 1

updated 2012-07-24 05:44:27 +0800

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.

link publish delete flag offensive edit

answered 2012-07-24 11:38:53 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

updated 2012-07-24 11:39:08 +0800

Hi, since zk 6.0.2, to wire component in a view model, you have to use new @AfterCompose annotation,
please read upgrade note : http://books.zkoss.org/wiki/Small_Talks/2011/November/ZK_6:_Upgrade_Notes#ZK_Bind_Since_6.0.2

link publish delete flag offensive edit

answered 2015-07-20 19:44:32 +0800

aktejo gravatar image 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,...

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-14 00:46:13 +0800

Seen: 2,576 times

Last updated: Jul 20 '15

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