2

How to Call Child ViewModel Method from Parent Window? [closed]

asked 2013-02-04 10:13:49 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

updated 2013-02-08 03:46:15 +0800

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

I have a ZUL page which included some other pages inside it and in my parent ZUL i have this scenario.

<window title="new page title" border="normal" id="inp"
        viewModel="@id('vm') @init('com.team.MyListbox')" apply="org.zkoss.bind.BindComposer" ctrlKeys="^a^s^d#f8" onCtrlKey="@command('ctrlKeyClick',item=event.getKeyCode())">

Now you can check i am calling a method ctrlKeyClick in parent viewmodel but somehow it is possible whenever user will use CTRL KEY I can pass keyCode in my Child View Method OR Can i call Child class method from inside Parent View Model. As we did in when we will want parent method from child viewmodel..

Binder bind = (Binder) view.getParent().getAttribute("binder");
        if (bind == null)
            return;
bind.postCommand("doSomething", params);

Can we do something like his in my my case?

delete flag offensive retag edit

The question has been closed for the following reason "the question is answered, right answer was accepted" by sjoshi
close date 2013-02-19 12:31:35

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-02-19 12:31:18 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

For this i used a different Approach . .i got another idea and look like it is working fine with me What i did? I made a Singleton Class and added a variable of Component class with get/set method. Now in each of my view Model afterCompose() i am calling setter method of Component variable from singleton class, and passing current viewModel Component object to it and in my HomeViewModel CTRL key event method i adding this code so it calling method from Selected tab .

Component ctrlkeyComp = idBinder.getCompObject();
if(ctrlkeyComp != null){
Binder bind = (Binder) ctrlkeyComp.getAttribute("binder");

if (bind == null)
return;
bind.postCommand("doActionInChildVM", map);

}
link publish delete flag offensive edit
0

answered 2013-02-05 09:31:10 +0800

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

updated 2013-02-08 03:32:27 +0800

use Global Command and BindUtils to listen to and post command between view model

For example,

ZUL

<window>
    <hlayout>
        <vlayout apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('simple.ViewModel1')">
            <button label="post1" onClick="@command('post1')"/>
            <textbox value="@bind(vm.value)"/>
            <label value="@bind(vm.value)"/>

        </vlayout>
        <vlayout apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('simple.ViewModel2')">
            <button label="post2" onClick="@command('post2')"/>
            <textbox value="@bind(vm.value)"/>
            <label value="@bind(vm.value)"/>
        </vlayout>
    </hlayout>
</window>

ViewModel 1

package simple;

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

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.NotifyChange;

public class ViewModel1 {

    String value = "model1";

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Command
    public void post1(){
        Map arg = new HashMap();
        arg.put("value", this.value);
        BindUtils.postGlobalCommand(null, null, "globalpost1", arg);
    }

    @GlobalCommand @NotifyChange("value")
    public void globalpost2(@BindingParam("value") String value){
        this.value = value;
    }
}

ViewModel 2

package simple;

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

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.NotifyChange;

public class ViewModel2 {

    String value = "model2";

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Command
    public void post2(){
        Map arg = new HashMap();
        arg.put("value", this.value);
        BindUtils.postGlobalCommand(null, null, "globalpost2", arg);
    }

    @GlobalCommand @NotifyChange("value")
    public void globalpost1(@BindingParam("value") String value){
        this.value = value;
    }
}
link publish delete flag offensive edit

Comments

Can we pass argument to Global Command

sjoshi ( 2013-02-05 09:33:57 +0800 )edit

I ahve done this thing by BindUtils.postGlobalCommand(null, null, "refresh", null);

sjoshi ( 2013-02-05 10:03:41 +0800 )edit

@sjoshi , if you think it solve this question, please rank the answer and close this qestion, thanks.

dennis ( 2013-02-08 03:34:16 +0800 )edit

Actually issue with Global Command is that if command name will appear more than one place it will call each global command

sjoshi ( 2013-02-08 04:56:56 +0800 )edit

Question tools

Follow
2 followers

RSS

Stats

Asked: 2013-02-04 10:13:49 +0800

Seen: 174 times

Last updated: Feb 19 '13

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