-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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????
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.
Asked: 2022-07-13 21:21:01 +0800
Seen: 6 times
Last updated: Jul 15 '22
bug with intboxes on mobile devices
zk keikai-how to add custom button/label to formulabar?
zk-keikai- update multiple cells parallel at same time asynchronously
zk-keikai-How to auto fit column width based on text
zk-keikai-ClipboardPateEvent-called twice
Reference a spring bean from VariableResolver