在 ZK 中,更改组件的 属性 来自其他 ViewModel

In ZK, change a component's property from other ViewModel

我在使用 ViewModel 的 zul 文件中有一个按钮。我需要 disable/enable 这个按钮,这取决于使用不同 ViewModel 的其他一些 zul 中的数据状态。 第一个 ZUL 文件:

<window apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('First VM')" validationMessages="@id('vmsgs')">
<tabbox>
  <tabs>
     <tab label="Label Value" />
  </tabs>
  <tabpanels>
     <tabpanel>
       <include someparameter="${some_VM_model_object}" src="ZUL2"></include>
     </tabpanel>
  </tabpanels>
</tabbox>
<button label="My Button" onClick="" id="mybutton" visible="false" />
</window>

现在又多了一个ZUL文件,和它对应的VM(假设它的VM是Second VM) 第二个虚拟机:

 @AfterCompose
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
        /*In this aftercompose method I want to check some condition on the model of the        
        second zul file and depending on that I want to disable/enable the "My Button"
        button in the  first ZUL*/    
    }

第一个 zul 和第二个 zul 可以有多个实例,但是相关的可以通过一些公共数据成员(在包含组件中作为 "someparameter" 传递)对象来识别视图模型。 这在 ZK 或任何有助于实现这一目标的方法中是否可行?

我将向您展示 ZK 的一些很棒的功能。

如果你的 zul2 在 zul 中有一个视图模型,那么 不要调用它 vm!
原因是实际上您的 zul2 可以访问 zul 1 的 VM。
所以你实际上可以在 Zul1VM 中编写 Zul2VM 的整个代码。

你不需要,如果 zul2 在没有 include 标签的情况下使用过,那么它就没有包含视图模型,所以它不能正常工作。

我会post给你举个例子,还有一个link到the fiddle with this code

第一个解法:

Index.zul

<window id="win" apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('pkg$.TestVM')">
    <include src="includePage.zul"  />
</window>

IncludePage.zul

<vbox>
    <label value="@load(vm.value)" />
    <button label="Update" onClick="@command('changeValue')" />
</vbox>

TestVM.java

public class TestVM {

    String value = "Default";

    public String getValue () {
        return value;
    }

    @Command
    @NotifyChange("value")
    public void changeValue () {
        value = "new value";
    }
}

第二种解法:

Read this documentation.

这对你意味着什么:

  1. 为您的包含提供一个 ID( 在文档中作为 window 作出反应)。
  2. 为您的组件提供 ID。
  3. 如果您的组件包含接口 Idspace 在第二个 zul 中,如果您的组件在树中,则需要对它们进行标识。
  4. 一个。 Path.getComponent("/zul2/minecomponent"); 如果之间没有 IdSpace 个分量。
  5. 乙。 Path.getComponent("/zul2/IdOfIdSpaceComponent/minecomponent"); 如果中间有 IdSpace 个分量。

请允许我说一句:

使用MVVM时不要使用@AfterCompose,那是对MVC的注解。 (我知道它在 MVVM 中有效)
正确的方法是使用 @Init 注解。 (你也可以说你的超类有一个 init to。)

编辑:

因为您仍然无法获取标签,所以我将解决方案 1 中的示例也更新为解决方案 2。
如您所见,我可以从包含的标签中获取值。
New fiddle can be found here..