0

Form binding with child collections - middle object not cloning collections?

asked 2012-05-24 17:36:32 +0800

radolang gravatar image radolang
12

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>

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2018-01-29 21:00:20 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Any example for this

link publish delete flag offensive edit

answered 2014-01-02 06:59:28 +0800

cor3000 gravatar image cor3000
6280 2 7

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

link publish delete flag offensive edit

answered 2013-06-13 23:22:29 +0800

radolang gravatar image radolang
12

It would be awesome if someone from ZK team commented on this. This is a common problem. Are we missing something obvious here?

link publish delete flag offensive edit

answered 2013-06-11 08:00:27 +0800

manzai gravatar image manzai
1

i have same problem like aznavour.

why this problem always no fix??

link publish delete flag offensive edit

answered 2013-01-16 17:19:46 +0800

aznavour gravatar image aznavour
26 3

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

link publish delete flag offensive edit

answered 2013-01-16 11:52:48 +0800

GEdelmann gravatar image GEdelmann
5 1

here is one more having the same problem...

link publish delete flag offensive edit

answered 2012-10-19 02:46:32 +0800

gubrak gravatar image gubrak
12

hi radolang, i have same problem with you...
have u resolved it?...

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
7 followers

RSS

Stats

Asked: 2012-05-24 17:36:32 +0800

Seen: 261 times

Last updated: Jan 29 '18

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