-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello,
There is the source:
textbox.zul
<zk xmlns:n="http://www.zkoss.org/2005/zk/native">
<window border="none" apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('se.spn.test.TextboxVM')"
validationMessages="@id('vmsgs')">
textbox: <textbox value="@load(vm.selection) @save(vm.selection, before='doCommand')" onChanging="@command('doCommand', val=self.value)" style="border-style:solid" width="100px"/>
</window>
</zk>
View Model TextboxVM.java:
public class TextboxVM {
private String selection;
public String getSelection() {
return selection;
}
public void setSelection(String selection) {
this.selection = selection;
}
@Command
public void doCommand(@BindingParam("val") String val) {
System.out.println("selection: " + selection + " value: " + val);
}
}
In debug mode doCommand is executed twice: the first time with old value, the second time with new one. In runtime it is executed once but with old value. selection is always the same.
Expectation: executed once with new value. selection gets new value before doCommand is executed
Please help
it is changing, the value did not update to component yet, use val=event.value
Example here
http://emrpms.blogspot.in/2012/05/text-box-on-change-and-on-changing.html
Hi Dennis,
event.value doesn't work. But it works if I get event value through ContextParam. I have prepared test with all possible ways, that I could come up, to pass value to ViewModel by using ZK 6 binding.
zul:
<window border="none" apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('se.spn.test.TextboxVM')">
textbox: <textbox id="t1" value="@load(vm.selection) @save(vm.selection, before='doCommand')" onChanging="@command('doCommand', self_value=self.value, event_value=event.value)" width="100px"></textbox>
</window>
view model:
public class TextboxVM {
private String selection;
@AfterCompose
public void doAfterCompose(@ContextParam(ContextType.VIEW) Component view) throws Exception {
Selectors.wireEventListeners(view, this);
}
public String getSelection() {
return selection;
}
public void setSelection(String selection) {
this.selection = selection;
}
@Command
public void doCommand(@BindingParam("self_value") String self_value, @BindingParam("self_value") String event_value,
@ContextParam(ContextType.TRIGGER_EVENT) InputEvent event) {
System.out.println("doCommand: selection: " + selection + " self_value: " + self_value + " event_value: "
+ event_value + " event_value2: " + event.getValue());
}
@Listen("onChanging=#t1")
public void onChanging(InputEvent event) {
System.out.println("onChanging=#t1: event_value3: " + event.getValue());
}
}
log:
doCommand: selection: self_value: event_value: event_value2: 1 onChanging=#t1: event_value3: 1
doCommand: selection: self_value: event_value: event_value2: 2 onChanging=#t1: event_value3: 2
doCommand: selection: self_value: event_value: event_value2: 23 onChanging=#t1: event_value3: 23
doCommand: selection: self_value: event_value: event_value2: 234 onChanging=#t1: event_value3: 234
doCommand: selection: self_value: event_value: event_value2: 2345 onChanging=#t1: event_value3: 2345
doCommand: selection: 2345 self_value: 2345 event_value: 2345 event_value2: 2345 onChanging=#t1: event_value3: 2345
As you can see 'save before doCommand' saved empty string to selection after I entered '1'. Then there is no updated data in selection variable and values that are passed by using self.value and event.value. But there is the information about update in @ContextParam variable and in listener @Listen("onChanging=#t1").
There is also problem with double invocation of doCommand and onChanging methods in VM when I use debug mode:
log:
1st invocation:
doCommand: selection: self_value: event_value: event_value2: 1 onChanging=#t1: event_value3: 1
doCommand: selection: 1 self_value: 1 event_value: 1 event_value2: 1 onChanging=#t1: event_value3: 1
2nd invocation:
doCommand: selection: 1 self_value: 1 event_value: 1 event_value2: 2 onChanging=#t1: event_value3: 2
doCommand: selection: 2 self_value: 2 event_value: 2 event_value2: 2 onChanging=#t1: event_value3: 2
Looks like it is done twice because after first time the value wasn't change properly and then textbox lost focus that allowed to see changes.
For me it looks like a bug
did you use zk ee, the event implicit object in zul is zkee only,
read this, http://books.zkoss.org/wiki/ZK%20Developer's%20Reference/MVVM/Advance/Parameters#Retrieve_Event_Object
No, I use zk 6.0.2 CE. That explains why event.value equals null all the time. Are there some difficulties in implementation to make onChanging event work as normal as onChange event? OnChange event doesn't require to pass event value to command to be able to save it in some view model attribute.
Also I have found that ckeditor has the same issue by not saving its value in view model before command is executed. But in this case onChange (not onChanging as for textbox) event doesn't work.
Asked: 2012-08-09 22:45:20 +0800
Seen: 642 times
Last updated: Aug 14 '12