-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi. I have a collection of values of different types, I'm constructing popup dynamically depending on this types. So I've wired parent component, and them I just create components like Textbox, Intbox etc and append them as children in Java.
Is it possible to configure data-bindind with elements of my value collection for brand new components from Java code?
Hi kityakuntsev,
Sounds like something you can solve in MVVM with templating. Simplest case for me is: iterate over template in a forEach loop. If your model contain both a type indicator and a value, I would pass both of those to a popup template, in which I would apply the appropriate sub-template for each popup "line" matching the requested type.
This is made very very easy by shadow elements (apply, forEach, - also choose/when if you have a really complex case with multiple attribute resolving to the same template). I think you could also do that with children binding (not as efficient as shadow, but hey)
I made a fiddle to illustrate the idea, have a look :) http://zkfiddle.org/sample/3u13r51/5-popup-mvvm-templating
a note on that example: I made absolutely no optimization on the popups there, since I just wanted to show how to build them. In that example, all the popups are build from the get-go and loaded with the page, because I didn't wanted to make it too complicated. In the real world, I'd try to only build when relevant and load the popup matching the current displayed content.
happy coding :)
Found a solution:
Intbox val = new Intbox();
if (value.getIntValue() != null) {
val.setValue(value.getIntValue());
}
val.addEventListener(Events.ON_CHANGE, new EventListener<Event>() {
@Override
public void onEvent(Event event) throws Exception {
value.setIntValue(Integer.valueOf(((InputEvent) event).getValue()));
}
});
Hey kityakuntsev,
Registering event listeners might not be the best option, since it's breaking the MVVM pattern separation between view and view model. Instead, you can register load / save binding and commands on your components in java.
Have a look here for the detailed article: https://www.zkoss.org/wiki/SmallTalks/2012/August/MVVMIn_Java
Generally speaking, you shouldn't create components in MVVM with some very specific exceptions. I don't know your use case in detail, but I would suggest looking at shadow elements if you want to swap between templates on the fly. http://books.zkoss.org/zk-mvvm-book/8.0/shadowelements/shadowelements.html It sounds to me like you could construct popups directly in zul. Can you send a small summary of your requirement and a code example?
Happy coding ;)
Hi MDuchemin, thanks for your answer.
My case is generating popup based on EAV data model. So I have different attribute types (like string, text, integer, decimal, dictionary), I have different attributes (or fields/inputs), and this is called EAV Metamodel and is stored in DB. Based on this metamodel I have to show popup with specified attributes, and inputs should depend on attribute's type. Each attribute has a constraint (like length limit, mandatory etc). When all attributes on popup filled, all values should be stored in DB (and this is EAV Object model)
There should be about 150 attributes in DB. I've tried using template-based views but I don't know the actual amount of attributes. So I need smth like model of components.
Wow this is gold! I've tried to use conditional templating and it was messy (because I'm not quite experienced with ZK), but your example is crazy and it works great.
Asked: 2017-10-27 16:15:25 +0800
Seen: 56 times
Last updated: Nov 01 '17
Attributes in macro was not updated after notifyChange
Problem binding values to a composite component
How can i load a constant in .zul
MVVM Validator: class not found ? [closed]
[Ugent please] connect Web service .net view in grid group ZK
Dirty state for forms with collection properties