# Use ViewModel in Composite Component

JustinFrost
139 1 6

I have defined a composite component that uses the following zul:

<div id="backpackWrap" apply="org.zkoss.bind.BindComposer" viewModel="@id('bvm') @init('foo.bar.CompView')">
<Component/>
<Component/>
</div>


When I use the composite component on the page, I get the following error:

org.zkoss.zk.ui.UiException: can't find Page to resolve a view model class :'foo.bar.CompView'

Is it possible to use a ViewModel inside the zul of a composite component ? If so how do I get this to work ?

Thanks,

delete retag edit

Sort by » oldest newest most voted

JustinFrost
139 1 6

Hi All,

I found a workaround for this, the composite component requires two zul files... Create an additional zull file for the composite component that is an include component with no src:

<include id="contentInclude"/>


In the composite component Java afterCompose method set the src of the include the the url of the zul that contains the view model and wire components again. Since the component is attached to a page before the include src is set everything works fine.

afterCompose(){
contentInclude.setSrc("path/to/zul/with/viewmodel.zul");
Selectors.wireVariables(this, this, null);
Selectors.wireComponents(this, this, false);
Selectors.wireEventListeners(this, this);

}

[hide preview]