Mrye
Mrye

Reputation: 727

ZK MVVM : Passing parameter to another viewmodel using <include>

What I want to do is passing value from page1.zul to page2.zul through <include> and viewModel.

From page1.zul, I have

<include processId="@bind(vm.selectedProcess.id)" src="@load('page2.zul')"></include>

And then it should pass to a viewModel in page2.zul

@Init
public void init(@ExecutionArgParam("processId") String processId){
    System.out.println("processInstanceId : " + processId);
}

However, I always get a null value. Any idea how to do this thing ? page2.zul looks something like this :

<div apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @bind('com.mrye.viewModel')">

    <label value="@load(vm.processId)"></label>
</div>

Upvotes: 2

Views: 4215

Answers (3)

dgofactory
dgofactory

Reputation: 348

Just be sure on VM1 the value vm.selectedProcess.id is correctly initialized and it has a value, in the VM2 add the @AfterCompose lifecycle annotation then on @Init get the value, according to your code this should work on VM2:

   Long processId;

   @AfterCompose
   public void initAfterCompose(@ContextParam(ContextType.VIEW) Component view) {
       Selectors.wireComponents(view, this, false);
   }

   @Init
   public void init() {
       //get dynamic attribut
       processId = (Long) Executions.getCurrent().getAttribute("processId");
   }

Upvotes: 0

Subodh Joshi
Subodh Joshi

Reputation: 13482

Here you can check

Index.zul

<?page title="URL Parameters Test" contentType="text/html;charset=UTF-8"?>  
<zk>  
<window title="URL Parameters Test" border="normal">  

    <include src="header.zul?test=5" />  

</window>  
</zk>  

In above code you can check it pass argument with URL here test is argument name and value=5

<?page contentType="text/html;charset=UTF-8"?>
<zk>
<window border="none" width="100%" height="100%" apply="pkg$.HeaderComposer">

<label id="lblHeader" />

<div>
Load from EL [ <label value="${param.test}" />]
</div>
</window>
</zk>

In this page we used ${param.test} to get the parameter passed in index.zul , HeaderComposer.java

import org.zkoss.zk.ui.*;  
import org.zkoss.zk.ui.event.*;  
import org.zkoss.zk.ui.util.*;  
import org.zkoss.zk.ui.ext.*;  
import org.zkoss.zk.au.*;  
import org.zkoss.zk.au.out.*;  
import org.zkoss.zul.*;  

public class HeaderComposer extends GenericForwardComposer{  


    Label lblHeader;  

    @Override  
    public void doAfterCompose(Component comp) throws Exception {  

        try {  
            super.doAfterCompose(comp);  
        }   
        catch (Exception e) {  
            e.printStackTrace();  
        }  

        /* 
         * retrieve url parameters 
         */  
        String[] parameter = (String[]) param.get("test");  

        if (parameter != null)  
            lblHeader.setValue( "Congratulations! Your parameters value is " + parameter[0] );  
        else  
            lblHeader.setValue( "No parameters found. URL should be something like http://yourserver/yoursite/main.zul?parameter=param-value" );  
    }  
}

for more way you can check http://zkframeworkhint.blogspot.in/2014/05/zk-include-how-to-pass-and-get.html

Upvotes: 1

chillworld
chillworld

Reputation: 4277

First of all, make distinguish names for Id's of the VM.

Then zul :

<div apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('com.mrye.viewModel', processId = parentVM.selectedProcess.id )">

And VM2:

@Init
public void init (@BindingParam("processId") MyObject processID) {

Edit after the comment :

As you can see in this fiddle it works, but your parameter has to be initialized.

If you want to have "live data" passed to other zul (not other viewmodel), you can use @ref or just use parentVM.
If the live data need's to be in the viewmodel itself, you can use a non visible textbox where you load the data from parentVM and save it in the includedVM just before some action happens.

See updated fiddle here.

Upvotes: 3

Related Questions