2

"NotifyChange" in subclass methods called from Parent will not fire [closed]

asked 2013-04-26 22:59:03 +0000

rickcr gravatar image 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>
delete flag offensive retag edit

The question has been closed for the following reason "the question is answered, right answer was accepted" by rickcr
close date 2013-04-27 00:33:40

Comments

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 +0000 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2013-04-26 23:54:16 +0000

Matze2 gravatar image Matze2
773 7

What I do is to call notifyChange programmatically via the binder.

In your superclass you can get the binder in Init method:

protected Binder binder;

@Init
public final void init(@ContextParam(ContextType.BINDER) Binder _binder) {
    binder = _binder;
}

and in the subclass you call, e.g. in your case

binder.notifyChange(this, "childVar");
link publish delete flag offensive edit

Comments

Brilliant! Thanks a ton. Is this documented somewhere? If not, it would be great if it was. (Note to anyone else following this in the future... be sure you subclass calls @Init(superclass=true) in order to fire the super class init as shown in Matze2's example.)

rickcr ( 2013-04-27 00:33:07 +0000 )edit

Right. I forgot this. And with "*" you can notify the change of all properties.

Matze2 ( 2013-04-27 00:47:45 +0000 )edit

In My Parent class,. the Init method never calls. why ?

Senthilchettyin ( 2016-12-18 09:04:48 +0000 )edit

Hi, maybe you forgot "@Init(superclass = true)" somewhere in your class hierarchy?

Matze2 ( 2016-12-19 07:20:23 +0000 )edit

Question tools

Follow

RSS

Stats

Asked: 2013-04-26 22:59:03 +0000

Seen: 104 times

Last updated: Apr 26 '13

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