0

Multiple ViewModels in a zul

asked 2021-01-26 04:55:28 +0800

RomanZK gravatar image RomanZK
1 1

updated 2021-01-26 12:53:39 +0800

cor3000 gravatar image cor3000
5868 2 7
ZK Team

For most of my zul pages I use mvvm. I declare a view model as follows

<window title="Contact List" border="normal"
         apply="org.zkoss.bind.BindComposer"
         viewModel="@id('vm') @init('com.MyVM')" hflex="1" vflex="1" contentStyle="overflow:auto">

I just discovered that it appears possible for each ui component to have it's own ViewModel in a zul page.

I will like to ask

  1. what are the usage scenarios for using more than one view model in a zul page ?

  2. is it possible for two view models in the same page to pass data to each other ?

or at least can a parent view model supply a parameter to child view model ?

If this is possible how can I declare such functionality in a zul ?

My understanding is that the parent view model is the view model I bind to my window ui component then other view models that I bind to other ui's e.g. grid in the page are the child view models.

I would appreciate clarity on the above as it can open up many possibilities as to how I develop my application.

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2021-01-26 13:01:48 +0800

cor3000 gravatar image cor3000
5868 2 7
ZK Team

updated 2021-01-26 13:02:13 +0800

There are so many options and this it not just "clarified" in a simple post ... so I'd suggest to specify a few requirements first.

A common scenario is having a Navigation view model and separate viewmodels dedicated to individual pages.

And yes, it's possible to pass data between view models - e.g. using global-commands or in case you have a reference to a viewmodel you can just call methods directly... after all view models are java objects.

link publish delete flag offensive edit

answered 2021-01-29 21:36:18 +0800

RomanZK gravatar image RomanZK
1 1

I am interested in knowing more about the scenarios you stated. i.e

  1. having a navigation view model and separate view models dedicated to individual pages.

  2. having a reference to a viewmodel you can just call methods directly.

can you share links to examples that illustrates this scenarios ?

link publish delete flag offensive edit

answered 2021-02-08 13:03:52 +0800

cor3000 gravatar image cor3000
5868 2 7
ZK Team

updated 2021-02-08 13:52:35 +0800

mvvm navigation: http://books.zkoss.org/zkessentials-book/master/navigationandtemplate/ajaxbasednavigation-mvvm.html

call VM method directly: https://zkfiddle.org/sample/238847/1-update-other-viewmodel-via-java

a command handler in the outerVM calls the doSomething method on the innerVM

link publish delete flag offensive edit

answered 2021-02-09 16:49:04 +0800

RomanZK gravatar image RomanZK
1 1

Thanks for the links. I will definitely go through and revert if I have further questions.

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

RSS

Stats

Asked: 2021-01-26 04:55:28 +0800

Seen: 21 times

Last updated: Feb 09

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