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.
-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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
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
}
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.
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.
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 )editedited 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 )editcause 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 )editAsked: 2014-03-21 12:44:17 +0800
Seen: 149 times
Last updated: Mar 24 '14