Editing somewhat complex object graphs in a form and automatic binding

144 views
Skip to first unread message

novi border

unread,
Nov 1, 2011, 4:30:33 PM11/1/11
to play-fr...@googlegroups.com
Hi,

I come from a Tapestry and GWT backgrounds so I am learning the play way of doing stuff..
I love it for simple pages and server apis, but now I am trying to make a more complex page,
which are not to hard with Tapestry or GWT, but I am learning play and would like to hear what
the play gurus recommend..

Task:
I want to have a single page in which to edit some interconnected objects. I want to have one
edit page with Parent object, which has many Child, which in turn have many Grandchild.
All objects are JPA entities. I am using jquery as a helper for showing/hiding parts of ui and other
simple stuff.

I would like to edit all of them as one unit, on the same page. As there are usually only few Children
per parent, and also Grandchildren, it seems that i could render a form with fields for all the needed
objects and subobjects, with most of them initially hidden from user. More details would be displayed
when the user clicks/expands portions of the form.

So far so good..

The part I am wandering about is how to structure form field ids to get the most from the automatic
binding. Ideally it would all bind to a single Parent instance in controller method which would also
understand ids of entities and perform insert, update or delete as needed. Or maybe bind to a Parent,
List<Child> and a List<Grandchild> and managing the JPA sync manually..

What would you recommend? Other approaches? Ajax seems like an overkill for this as the amount
of data is not too large and it would require having the current object graph stored somewhere on
the server, probably the cache, and having it synchronized withe the database on save. I kind
of prefer simple html form and updating in a single step where all the state is stored in the form.

NB

Sebastien Cesbron

unread,
Nov 2, 2011, 5:14:05 AM11/2/11
to play-fr...@googlegroups.com
Hi

I experienced some problem with this kind of page and automatic binding because automatic binding automagically reattached entities to the EM.

The only way I found to handle this situation is to use indexed notation for child objects. Some kind of

#{list parent.children, as: 'child'}
<input type="hidden" name="parent.children[${child_index - 1}].id" id="${child.id}"/>
#{/list}

With this notation, it works great but this can be tricky to handle with jquery if you want to be able to add/remove child. The other notation "parent.children.id" does not work with onetomany relations.

Regards
Seb

2011/11/1 novi border <novib...@gmail.com>


NB

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/W9W_DfeDt2UJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

novi border

unread,
Nov 2, 2011, 8:47:46 AM11/2/11
to play-fr...@googlegroups.com
Yes, after some testing I reached the same conclusion.
I am going to do something similar to what you say, binding main object and two separate lists from my form and then detecting inserts/updates/deletes and merging with main JPA entity.
I think it is not too complex to do the insertion and deletion with jquery, but for insertion I have to duplicate templates for jquery.
Deleted items from sublist will simply bind to a null item in the list in controller, and for inserting items the page renders next indices for arrays which should be used by jquery for insertion of respective items in the form...

Reply all
Reply to author
Forward
0 new messages