0

Textbox onChanging event doesn't work properly

asked 2012-08-09 22:45:20 +0800

stream gravatar image stream
81 3

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

delete flag offensive retag edit

8 Replies

Sort by ยป oldest newest

answered 2012-08-10 00:59:48 +0800

dennis gravatar image dennis
3669 1 6
http://www.javaworld.com....

updated 2012-08-10 03:27:24 +0800

it is changing, the value did not update to component yet, use val=event.value

link publish delete flag offensive edit

answered 2012-08-10 09:39:09 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2615 3 8
http://emrpms.blogspot.in...

Example here
http://emrpms.blogspot.in/2012/05/text-box-on-change-and-on-changing.html

link publish delete flag offensive edit

answered 2012-08-10 17:33:50 +0800

stream gravatar image stream
81 3

updated 2012-08-10 17:36:25 +0800

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

link publish delete flag offensive edit

answered 2012-08-10 17:37:32 +0800

stream gravatar image stream
81 3

Thanks Senthilchettyin for an example. I am interested to make it work by using zk 6 databinding

link publish delete flag offensive edit

answered 2012-08-11 02:01:38 +0800

dennis gravatar image dennis
3669 1 6
http://www.javaworld.com....

updated 2012-08-11 02:01:57 +0800

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

link publish delete flag offensive edit

answered 2012-08-11 05:10:49 +0800

stream gravatar image stream
81 3

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.

link publish delete flag offensive edit

answered 2012-08-11 05:13:47 +0800

stream gravatar image stream
81 3

updated 2012-08-11 05:16:48 +0800

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.

link publish delete flag offensive edit

answered 2012-08-14 15:38:37 +0800

stream gravatar image stream
81 3

Dennis, are there any plans to make it work for CE version?

link publish delete flag offensive edit
Your reply
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

RSS

Stats

Asked: 2012-08-09 22:45:20 +0800

Seen: 620 times

Last updated: Aug 14 '12

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