asked
2013-04-26 22:59:03 +0800
rickcr 704 ● 7 This is quite frustrating, and I'm hoping there is a work around. Following good "Don't Repeat Yourself" principles, I'm trying to push some common functionality for some view models into a Base ViewModel. When the base class method is triggered (@Comamnd) it does its thing and then calls a "postProcess" method that all subclasses must implement so that they can do extra work if need be.
The problem is ZK requires all "NotifyChange" params to be handled on the very initial Command method. This is problematic since this method is in a base class and it shouldn't have to know about every subclass field that has to be notified.
(I was trying to illustrate this in Zk Fiddle but it seems to have some issues using inheritance there.) So here is the use case:
If you run the following you'll never get "childVar" to show up unless you move "childVar" as part of the NotifyChange on the base class.
ParentVM
public abstract class ParentVM {
protected String parentVar;
//Don't want to have to do @NotifyChange({"parentVar","childVar"})
@NotifyChange({"parentVar"})
@Command
public void process() {
parentVar = "Foo"+ System.currentTimeMillis();
postProcess();
}
public abstract void postProcess();
public String getParentVar() {
return parentVar;
}
public void setParentVar(String parentVar) {
this.parentVar = parentVar;
}
}
ChildVM
public class ChildVM extends ParentVM {
private String childVar;
//childVar never notified since call is from Base class!
@NotifyChange("childVar")
@Override
public void postProcess() {
childVar = "childVar-"+System.currentTimeMillis();
}
public String getChildVar() {
return childVar;
}
public void setChildVar(String childVar) {
this.childVar = childVar;
}
}
ZUL
<zk>
<window apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('zktesting.ChildVM')">
<vlayout>
<hlayout><label value="parent:"/><textbox value="@bind(vm.parentVar)"/></hlayout>
<hlayout><label value="child:"/><textbox value="@bind(vm.childVar)"/></hlayout>
<button onClick="@command('process')" label="Process"/>
</vlayout>
</window>
</zk>
If someone else get's here searching, please vote for this to be looked at by the devs here: http://tracker.zkoss.org/browse/ZK-1744?focusedCommentId=15714
rickcr ( 2013-05-30 18:44:01 +0800 )edit