1

ZK7 MVVM update binded bean property while set value programmatically

asked 2014-09-04 08:15:31 +0800

KMaria gravatar image KMaria flag of Russian Federation
30 3

updated 2014-09-05 07:30:31 +0800

Hello! How can I update bean property by setting value to component programmatically without direct call of setter?

I have three depended textboxes, when value of one of them changed, the other values should be calculated and changed as well. All textboxes binded to POJO bean properties.

ZUL code:

<label   id="weightKgLabel" value="Weight, kg"/>
<textbox id="weightKgTextbox" value="@bind(ivm.ware.weight)" onChange="@command('calculateWeights', currentWeight = self, fellow1 = weightLbTextbox, fellow2 = weightOzTextbox)">
    <custom-attributes measureUnit="${kg}"/>
</textbox>
<label   id="weightLbLabel" value="Weight, lb"/>
<textbox id="weightLbTextbox" value="@bind(ivm.ware.weightLb)" onChange="@command('calculateWeights', currentWeight = self, fellow1 = weightKgTextbox, fellow2 = weightOzTextbox)">
    <custom-attributes measureUnit="${lb}"/>
</textbox>
<label   id="weightOzLabel" value="Weight, oz" hflex="1"/>
<textbox id="weightOzTextbox" value="@bind(ivm.ware.weightOz)" onChange="@command('calculateWeights', currentWeight = self, fellow1 = weightLbTextbox, fellow2 = weightKgTextbox)">
    <custom-attributes measureUnit="${oz}"/>
</textbox>

Setters in bean class:

public void setWeight(Double weight) {
    this.weight = weight;
    context.createPath("/xml/Weight", weight);
}

public void setWeightOz(Double weight) {
    this.weightOz = weight;
    context.createPath("/xml/WeightOunces", weight);
}

public void setWeightLb(Double weight) {
    this.weightLb = weight;
    context.createPath("/xml/WeightPounds", weight);
}

Here is my calculation method in ViewModel class:

@Command
public void calculateWeights(@BindingParam("currentWeight") Textbox weightTextbox, @BindingParam("fellow1") Textbox fellowTextbox1, @BindingParam("fellow2") Textbox fellowTextbox2)
{
    UID meuWeightUID = (UID)weightTextbox.getAttribute("measureUnit");
    Double koef = MEU.getKoef(meuWeightUID);
    Double valueInKg = Double.parseDouble(weightTextbox.getValue()) * koef;

    UID meuFellow1UID = (UID)fellowTextbox1.getAttribute("measureUnit");
    Double fellowKoef1 = MEU.getKoef(meuFellow1UID);
    double fellowValue1 = Commons.round(valueInKg / fellowKoef1, 2);
    fellowTextbox1.setValue(fellowValue1 + "");

    UID meuFellow2UID = (UID)fellowTextbox2.getAttribute("measureUnit");
    Double fellowKoef2 = MEU.getKoef(meuFellow2UID);
    double fellowValue2 = Commons.round(valueInKg / fellowKoef2, 2);
    fellowTextbox2.setValue(fellowValue2 + "");
}

So, when the calculateWeights is called any 1 of 3 is set on client side and any 2 of 3 is set programmatically. The field set on client side calls corresponded setter perfect. But the problem is that fellowTextbox1.setValue("...") neither fellowTextbox2.setValue("...") do not call their binded setter. What should be done to fix it?

Thanks in advance!

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-09-04 14:35:13 +0800

chillworld gravatar image chillworld flag of Belgium
5337 4 9
https://github.com/chillw...

updated 2014-09-04 14:42:58 +0800

Zul :

<textbox id="weightKgTextbox" value="@bind(ivm.ware.weight)" onChange="@command('calculateWeights', measure = 'kg')">

Java :

@Command
@NotifyChanged("ware")
public void calculateWeights(@BindingsParam("measure") String measure) {
    if ("kg".equals(measure)) {
        // Do the calc
        ware.setWeightOz(...); // result calc
        // Do the calc
        ware.setWeightLb(...); // result calc
    }
    if ("oz".equals(measure)) {
        // Do the calc
        ware.setWeight(...); // result calc
        // Do the calc
        ware.setWeightLb(...); // result calc
    }
    if ("lb".equals(measure)) {
        // Do the calc
        ware.setWeight(...); // result calc
        // Do the calc
        ware.setWeightOz(...); // result calc
    }
}

Just a quick solution. I suggest creating 3 commands for each specific change so you don't have to play with the bindingparam. :

zul :

<textbox id="weightKgTextbox" value="@bind(ivm.ware.weight)" onChange="@command('calculateKg')">

java:

@Command
@NotifyChanged("ware")
public void calculateKg) {
    // Do the calc
    ware.setWeightOz(...); // result calc
    // Do the calc
    ware.setWeightLb(...); // result calc
}
link publish delete flag offensive edit

Comments

Makes sense, thank you! Sometimes trying to find "beautiful" solution we loose easy one :)

KMaria ( 2014-09-05 08:20:40 +0800 )edit

your welcome

chillworld ( 2014-09-05 08:39:25 +0800 )edit
Your answer
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
1 follower

RSS

Stats

Asked: 2014-09-04 08:15:31 +0800

Seen: 30 times

Last updated: Sep 05 '14

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