Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the root component of a given component in zk?

Tags:

java

zk

In my viewmodel I have the following code:

@Command
public void onFinish(@BindingParam(value = "myButton") Button myButton) {
        Component root = myButton.getParent().getParent().getParent();
        ...
}

Is there a more elegant way to find the root component of any given component? The expression above has to be altered every time I change the zul.

P.S. I am new to zkoss..:)

like image 725
FlowerPower71 Avatar asked Dec 06 '25 03:12

FlowerPower71


1 Answers

It all depends on what the root of your component is.

Your root is normally an IdSpace, and if you didn't have set other IdSpace element's you could use :

@Command
public void onFinish(@ContextParam(ContextType.SPACE_OWNER) IdSpace spaceOwner) {

    ...
}

This way, you never have to pass anything in the zul.
It will automaticly fetch that component. Remember that IdSpace is an interface what is implemented in some specific components.

Please take a look of the other, maybe more suitable in your case, options you can do.

But there are also other tricks, this one uses CSS selectors :

@Command
public void onFinish(@SelectorParam(":root") Component root) {
    ...
}

Like this you will always have the root.

I'm not a great fan of sending components from the view to the viewmodel with bindingparams.
Reason is that today, you call this command from a button, but maybe you add the same command on other component and then this will fail.
Yes, you can use the Component class, but MVVM has so many ways to do it in a better way without polluting your view.
Remember that the whole point of MVVM is a separation between UI and code.
It may not matter what triggers a command, even as it doesn't matter what your collection will fill on the screen.

like image 57
chillworld Avatar answered Dec 07 '25 23:12

chillworld



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!