0

middle object buffer support for HashMap

asked 2020-09-29 21:19:08 +0800

RomanZK gravatar image RomanZK
1

updated 2020-09-30 10:34:59 +0800

hawk gravatar image hawk
2655 1 5
http://hawkphoenix.blogsp... ZK Team

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,

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2020-09-30 11:01:27 +0800

hawk gravatar image hawk
2655 1 5
http://hawkphoenix.blogsp... ZK Team

updated 2020-09-30 11:02:23 +0800

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?

link publish delete flag offensive edit

answered 2020-09-30 12:12:10 +0800

RomanZK gravatar image RomanZK
1

Hi ! I think this should be possible. I will try it out and share my findings.

Thanks for the suggestion.

I will revert.

link publish delete flag offensive edit

answered 2020-10-02 21:17:04 +0800

RomanZK gravatar image RomanZK
1

I have attempted your work around i.e. to use vm.currentUser.attributes

It still did not work.

link publish delete flag offensive edit

answered 2020-10-07 11:44:02 +0800

hawk gravatar image hawk
2655 1 5
http://hawkphoenix.blogsp... ZK Team

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

link publish delete flag offensive edit

answered 2020-10-07 14:05:38 +0800

RomanZK gravatar image RomanZK
1

updated 2020-10-08 12:35:00 +0800

cor3000 gravatar image cor3000
5399 2 7
ZK Team

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.

link publish delete flag offensive edit

answered 2020-10-08 12:34:01 +0800

cor3000 gravatar image cor3000
5399 2 7
ZK Team

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.

link publish delete flag offensive edit

answered 2020-10-08 12:57:29 +0800

RomanZK gravatar image RomanZK
1

Thanks for sharing this.

I will download the source code and compare with what I implemented.

I am happy to see that this actually works.

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
1 follower

RSS

Stats

Asked: 2020-09-29 21:19:08 +0800

Seen: 17 times

Last updated: Oct 08

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