-
FEATURED COMPONENTS
First time here? Check out the FAQ!
How are we supposed to work with child collections in form binding? Below is an example, which uses form binding and multi select listbox bound to the child collection using selectedItems (plural) attribute. When the form middle object is created it has a "children" property as expected, but it is referencing the original set from the viewmodel bean. So even before I submit the form, my collection of children in view model bean has changed - the form middle object is not functioning as a buffer for dirty data in this case. I could not find any documentation for this..
Is there any recommended way to bind child collections when using form binding?
Thanks,
Rado
Pseudo code of my classes:
class Parent { String name; Set<Child> children; getName() {...} setName(String name); getChildren() {...} setChildren(children) {...} } class Child { String name; getName() {...} setName(String name); } class ParentVM { Parent parent = new Parent(); List<Child> allChildren = Child.findAll(); getParent() {...} setParent(Parent parent) {...} getAllChildren() {...} @Command public void save() {...} }
My partial zul page:
<div viewModel="@id('vm') @init('ParentVM')" apply="org.zkoss.bind.BindComposer"> <vlayout form="@id('fx') @init(vm.parent) @save(vm.parent, before='save')"> <textbox value="@load(fx.name)"/> <listbox model="@load(vm.allChildren)" selectedItems="@bind(fx.children)" checkmark="true" multiple="true"> <template name="model"> <listitem label="@load(each.name)" /> </template> </listbox> <button onClick="@command('save')"/> </vlayout> </div>
Any example for this
Hi,
the topic is addressed in this Ticket (http://tracker.zkoss.org/browse/ZK-2078). As long as there are no items added to or removed from the collection it is possible to use nested forms for each element in the collection (see comment in jira)
The other way is to clone the object manually before changing it. As deep-cloning is not a trivial topic that can be handled in general (http://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object-in-java)
Btw, the current Form Cache implementation does not clone the Object, it just keeps track of the bindings, and changed values before applying them on save. e.g.: a binding to "@bind(fx.children[0].name)" would work. The problem occurs in case of the model/template iteration using "each" which is unaware of being inside a form. Even if this would be resolved, adding and removing elements from/to the list would remain a problem.
Regards,
Robert
Similar problem here. We're using a custom (composite) component that contains a Listbox. The Form binding does only seem to load the items. Saving back to the middle object does not seem to work as our validator doesnt find anything in the validationContext for the name of the collection. All simple fields seem to work so far as expected, but not collections.
Please help us!
Charles
Asked: 2012-05-24 17:36:32 +0800
Seen: 261 times
Last updated: Jan 29 '18