Revision history [back]

click to hide/show revision 1
initial version

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

dennis gravatar image dennis

http://www.javaworld.com....

use (Global Command)[http://books.zkoss.org/wiki/ZKDeveloper%27sReference/MVVM/DataBinding/GlobalCommand_Binding} to listen and post command between view model

use (Global Command)[http://books.zkoss.org/wiki/ZKDeveloper%27sReference/MVVM/DataBinding/GlobalCommand_Binding} to listen to and post command between view model

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;
    }
}

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

for 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;
    }
}
Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More