1

Notify Form of property change

asked 2014-03-21 12:44:17 +0800

JMVAZ gravatar image JMVAZ
13 3

Dear all,

I'm facing a problem and I need some precious help. :)

In Zul, I had declared one form which is initialized in the Viewmodel. Example:

<zk>
<div apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @init('demo.viewmodels.TestViewModel')"
    form="@id('fx') @load(vm.product) @init(vm.myForm) @save(vm.product, before='submit')">

        <vlayout>
            <textbox value="@bind(fx.name)"/>
            <textbox value="@bind(fx.description)"/>
            <doublebox value="@bind(fx.price)"/>
        </vlayout>

        <button id="load_product" label="Select Most Recent Product" onClick="@command('select_recent')"/>
        <button label="Submit" onClick="@command('submit')" />
</div>

</zk>

In the button with id "loadproduct", I want to automatically fill some inputs. When I click in the button the command 'selectrecent' in called, but the notify change declared in the viewmodel, doesn't update the fields binded to the middle object "fx". My viewmodel:

public class TestViewModel {

private Form myForm = new SimpleForm();
private Product product = new Product();

@Command("select_recent")
@NotifyChange("myForm")
public void selectRecent(){
    myForm.setField("name", "recent_product_name");
    myForm.setField("description", "recent_product_name");
    myForm.setField("price", new Double(100.0));
}

@Command("submit")
public void submit(){
    System.out.println( "name:" + product.getName());
    System.out.println( "description:" + product.getDescription());
    System.out.println( "price:"+ product.getPrice());
}

public Form getMyForm() { return myForm; }
public Product getProduct() {return product;}
public void setProduct(Product product) {this.product = product;}

}

This example posted, is a very simple example. In fact what I want to do is much more complex. I have one parent form which is filled, using the help of some modal windows. All the changes done, must be reflected in the parent form, and the save of all the inputs only can be done on the submit of the parent form. The technical solution must use one middle object, because I will have the edition mode of the form, and all the changes done cannot be reflected in the original object, until the confirm is pressed.

Waiting one reply,

Thanks for your time.

Jaime

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-03-21 15:14:38 +0800

sitansu gravatar image sitansu
2254 13
http://java91.blogspot.in...

updated 2014-03-21 15:15:27 +0800

You just try it:

@NotifyChange({"myForm"})
@Command("select_recent")
public void selectRecent(){
    myForm.setField("name", "recent_product_name");
    myForm.setField("description", "recent_product_name");
    myForm.setField("price", new Double(100.0));
BindUtils.postNotifyChange(null, null, TestViewModel .this, "myForm");// here notify 
}
link publish delete flag offensive edit

Comments

Actually this won't do a thing more or less. @NotifyChange works on commands and setters. This is clearly a command so myForm is notified.

chillworld ( 2014-03-22 20:40:14 +0800 )edit
0

answered 2014-03-22 05:29:31 +0800

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

updated 2014-03-24 10:42:08 +0800

Remove the private SimpleForm from your vm and make it a bindingparam. Remove also the @init(vm.myForm) from your zul.

Now change your zul and controller to this :

<button id="load_product" label="Select Most Recent Product" onClick="@command('select_recent', product = fx)"/>

and in your vm it will result in :

@Command("select_recent")
public void selectRecent((@BindingParam("product") SimpleForm fx){
    fx.setField("name", "recent_product_name");    
    fx.setField("description", "recent_product_name");    
    fx.setField("price", 100.0);
    BindUtils.postNotifyChange(null, null, fx, "*");
}

Greetz chill.

link publish delete flag offensive edit

Comments

If I set the product directly with the values, I will not use the concept of middle object. What I will do when cancel button is clicked instead of submit? How can I rollback the unwanted changes of the product? The use of the BindingParam is wrong, fx is one SimpleForm and not a Product.

JMVAZ ( 2014-03-24 09:27:17 +0800 )edit

edited answer to a correct one, wasn't aware that fx was simpleForm :) (tested the code myself and it works)

chillworld ( 2014-03-24 10:35:44 +0800 )edit

Did it work or do you have still a problem?

chillworld ( 2014-03-24 12:36:40 +0800 )edit

Now the values are updated, but the passing the form by parameter is redundant, because the Form is initialized in VM. Why you advise to remove the init of the form from the viewmodel?

JMVAZ ( 2014-03-24 14:31:43 +0800 )edit

cause you don't need it to keep it in the VM, just pass it along with the command. (I also said to remove the simpleForm variable from you vm)

chillworld ( 2014-03-24 14:46:31 +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-03-21 12:44:17 +0800

Seen: 128 times

Last updated: Mar 24 '14

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