0

Listbox: Modify Listitem children in @AfterCompose

asked 2022-07-13 21:21:01 +0800

KlausWr gravatar image KlausWr
37 5

updated 2022-07-13 21:25:04 +0800

I have an interesting question:

If I create a listbox in a zul in MVVM style like:

<listbox id="myListbox" model="@bind(vm.list)">
 <template name="model">
    <listitem>
        <listcell>
            <textbox value="@bind(each)"/>
        </listcell>
 </template>
</listbox>

And want to modify the Textbox component (e.g. by adding a dynamic constraint) in the java ViewModel:

@Wire Listbox myListbox; ...

@AfterCompose

public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {

Selectors.wireComponents(view, this, false);
TextBox tb = myListbox.get????
tb.setConstraint("no empty");

So, how do I access the Textbox component????

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-07-15 10:11:55 +0800

MDuchemin gravatar image MDuchemin
2146 1 5
ZK Team

Hi there,

Generally speaking, you shouldn't access the textbox component in MVVM. :D Accessing components in the VM is breaking the separation between the view and the viewModel.

Instead, you should make the constraint available on the viewModel, and you bind it to the constraint field of the textbox.

<textbox value="@bind(each)" constraint="@load(vm.myConstraint)"/>

Alternatively, if you have unique constraints per textbox, you can use a bean containing both the value and the constraint for each line (and any other property for that line), then have something like:

<textbox value="@bind(each.value)" constraint="@load(each.constraint)"/>

In both case, you can perform notifyChange on the constraint to change it dynamically.

Additionally, if you have a lot of Java processing to do when rendering a row, you can consider using a ListitemRenderer to render each row, instead of a template. That way, you can build the listitem from java without breaking the MVVM pattern.

And lastly, it is possible to @Wire components in ViewModel as you already know, but it's always the worse way to do MVVM, will probably cause weird stuff to happen due to the binder and the developer modifying the same properties at the same time, and it breaks the MVVM pattern.

link publish delete flag offensive edit
Your answer
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: 2022-07-13 21:21:01 +0800

Seen: 6 times

Last updated: Jul 15

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