-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Good Day,
I am currently learning (evaluating) ZK for a web project. I am on chapter 3 of the Zk essentials.
I find the MVC and MVVM approaches very good.
For some of my use cases the persistence engine can return data as hashmap instead of javabean entities.
As a result for some use cases I will not use java bean as my data transfer object (DTO) instead I will use a map of attributeBindings. for example
HashMap<string, attributedto=""> customerData = new HashMap<string, attributedto="">();
where AttributeDTO is defined as follows:
Class AttributeDTO
public Object getInputValue();
public void setInputValue(final Object object);
AttributeDTO conforms to the java bean specification. (setters and getters)
Since ZK supports java ee EL 2.2. I discovered that I could bind my custom data model as follows
<row>
<cell sclass="row-title">Full Name :</cell>
<cell><textbox value="@bind(vm.currentUser.fullName.inputValue)" constraint="no empty: Plean enter your full name" width="200px"/></cell>
</row>
Where vm.currentUser is a class that implements the java.util.Map interface so it is a map.
fullName is a key that allows to retrieve AttributeDTO then the "inputValue" property can be read and or written to.
note: I created my own custom dummy data model based on the profile mvvm example in chapter 3
It actually works. I could read and write.
However, I am currently on the section "Keep unsaved input away"
I am unable to use the middle object as buffer for my custom data model. It does not work
<grid width="500px" form="@id('fx') @load(vm.currentUser) @save(vm.currentUser, before='save')">
<columns>
<column align="right" hflex="min"/>
<column/>
</columns>
<rows>
<row>
<cell sclass="row-title">Account :</cell>
<cell><label value="@load(fx.account.inputValue)"/></cell>
</row>
<row>
<cell sclass="row-title">Full Name :</cell>
<cell>
<textbox value="@bind(fx.fullName.inputValue)" width="200px"
constraint="no empty: Plean enter your full name" />
</cell>
</row>
.....
None of the fields display. My guess is that for the buffer object the only supported data type is java bean. is that the case ?
Is there a work around for implementing same with my custom map based data model ?
Thanks,
Yes, it's a known issue: https://tracker.zkoss.org/browse/ZK-3348
The form binding object fx
can't be a Map
, but its property can be a Map
. Since zk 8, ZK creates a form binding object with a proxy.
The workaround is to wrap a map with a bean, e.g. vm.currentUser.attributes
is a Map
. Is that possible for you?
Do you remove Map
from vm.currentUser
? If vm.currentUser
is a Map, ZK doesn't create a proxy for it.
To use form binding with a collection, please read "Form proxy Object" at http://books.zkoss.org/zk-mvvm-book/9.5/databinding/formbinding.html
Yes I did.
vm.currentUser is no longer a map. It is now a java bean that has a getAttributes() method that returns a map.
I created a class called "RecordBinding". This class is a java bean. This class has a map called "attributes". I also implemented a getAttributes() method that returns a map.
In my RecordBinding
Class RecordBinding
private HashMap attributes;
----------
// getter for returning map
public HashMap getAttributes()
In my vm
Class MyViewModel
private RecordBinding currentUser;
-------
// getter method returns recordBinding which is a java bean.
public RecordBinding getCurrentUser()
I then tried vm.currentUser.attributes in zul. It does not work.
Here a runnable example on zkfiddle.
It shows how the a map can be used in a form binding. As you can see the form is bound to currentUser
(which is a regular java bean) and the attributes
map is a member of this bean. Then the dynamic map properties can be used in a form binding.
The save
command will commit the changes to the original object. the cancel command will revert the values from the original object.
Asked: 2020-09-29 21:19:08 +0800
Seen: 17 times
Last updated: Oct 08 '20