Wire Variables"

From Documentation
m ((via JWB))
m ((via JWB))
Line 39: Line 39:
 
First, you should register variable resolvers. There are two approaches to register a variable resolver: the <javadoc type="interface">org.zkoss.zk.ui.select.annotation.VariableResolver</javadoc> annotation or [[ZUML Reference/ZUML/Processing Instructions/variable-resolver|the variable-resolver directive]]. Here is the example of registering variable resolvers with annotations.
 
First, you should register variable resolvers. There are two approaches to register a variable resolver: the <javadoc type="interface">org.zkoss.zk.ui.select.annotation.VariableResolver</javadoc> annotation or [[ZUML Reference/ZUML/Processing Instructions/variable-resolver|the variable-resolver directive]]. Here is the example of registering variable resolvers with annotations.
  
<source lang="java" high="1">
+
<source lang="java" highlight="1">
 
@VariableResolver({foo1.MyResolver.class, foo2.AnotherResolver.class})
 
@VariableResolver({foo1.MyResolver.class, foo2.AnotherResolver.class})
 
public class FooViewModel{
 
public class FooViewModel{
Line 48: Line 48:
 
Then annotate fields or methods with the <javadoc type="interface">org.zkoss.zk.ui.select.annotation.WireVariable</javadoc> annotation. For example,
 
Then annotate fields or methods with the <javadoc type="interface">org.zkoss.zk.ui.select.annotation.WireVariable</javadoc> annotation. For example,
  
<source lang="java" high="3,5">
+
<source lang="java" highlight="3,5">
 
@VariableResolver({foo1.MyResolver.class, foo2.AnotherResolver.class})
 
@VariableResolver({foo1.MyResolver.class, foo2.AnotherResolver.class})
 
public class FooViewModel {
 
public class FooViewModel {
Line 64: Line 64:
 
If you'd like to wire the Spring-managed beans, you usually register the Spring variable resolver, <javadoc> org.zkoss.zkplus.spring.DelegatingVariableResolver </javadoc>. Then, you could annotate <code>@WireVariable</code> for wiring a Spring managed bean. For example,
 
If you'd like to wire the Spring-managed beans, you usually register the Spring variable resolver, <javadoc> org.zkoss.zkplus.spring.DelegatingVariableResolver </javadoc>. Then, you could annotate <code>@WireVariable</code> for wiring a Spring managed bean. For example,
  
<source lang="java" high="1,3">
+
<source lang="java" highlight="1,3">
  
 
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
 
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
Line 78: Line 78:
 
Notice that the variables are wired before instantiating the component and its children, so you can use them in EL expressions. For example, assume we have a ViewModel as follows.
 
Notice that the variables are wired before instantiating the component and its children, so you can use them in EL expressions. For example, assume we have a ViewModel as follows.
  
<source lang="java" high="1,3">
+
<source lang="java" highlight="1,3">
  
 
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
 
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)

Revision as of 06:01, 12 January 2022

Stop.png This article is out of date, please refer to zk-mvvm-book/8.0/data_binding/advanced/wire_variables for more up to date information.


Wire Variables

We can use @WireVariable to wire variables from implicit objects or registered variable resolvers in a ViewModel as we do in a composer (please refer to ZK Developer's Reference/MVC/Controller/Wire Variables). Because BindComposer wires those variables for us before calling initial method. But it will not wire components and listeners automatically like a composer. To achieve it, please refer to ZK Developer's Reference/MVVM/Advance/Wire Components and ZK Developer's Reference/MVVM/Advance/Wire Event Listeners.


Wire from Implicit Objects

Wiring from implicit object is equivalent to calling Components.getImplicit(Page, String), by the name specified on @WireVariable. If the name is absent and the field or method parameter is of type Execution, Page, Desktop, Session, or WebApp, it still will be wired to the correct implicit object. However, in other cases, an exception will be thrown.

public class FooViewModel{
	
	@WireVariable
	private Page _page;
	
	@WireVariable
	private Desktop _desktop;
	
	@WireVariable
	private Session _sess;
	
	@WireVariable
	private WebApp _wapp;
	
	@WireVariable("desktopScope")
	private Map<String, Object> _desktopScope;

}


Wire from Variable Resolver

First, you should register variable resolvers. There are two approaches to register a variable resolver: the VariableResolver annotation or the variable-resolver directive. Here is the example of registering variable resolvers with annotations.

@VariableResolver({foo1.MyResolver.class, foo2.AnotherResolver.class})
public class FooViewModel{
....
}

Then annotate fields or methods with the WireVariable annotation. For example,

@VariableResolver({foo1.MyResolver.class, foo2.AnotherResolver.class})
public class FooViewModel {
    @WireVariable
    Department department;

    @WireVariable
    public void setManagers(Collection<Manager> managers) {
        //...
    }
}

Wire Spring-managed Beans

If you'd like to wire the Spring-managed beans, you usually register the Spring variable resolver, DelegatingVariableResolver. Then, you could annotate @WireVariable for wiring a Spring managed bean. For example,

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class PasswordViewModel{
    @WireVariable
    private User user;

}

DelegatingVariableResolver is a variable resolver used to retrieve the Spring-managed bean, so the variable will be retrieved and instantiated by Spring.

Notice that the variables are wired before instantiating the component and its children, so you can use them in EL expressions. For example, assume we have a ViewModel as follows.

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class UserViewModel {
    @WireVariable
    private List<User> users;

    public ListModel<User> getUsers() {
        return new ListModelList<User>(users);
    }
}

Then, you could bind it in the ZUL document. For example,

    <grid model="@load(vm.users)">

You might make a ViewModel as a Spring bean then other beans used by the ViewModel can be injected by Spring, but it is not recommended. Please refer to ZK Developer's Reference/MVC/Controller/Wire Variables#Warning:_Not_a_good_idea_to_have_Spring_managing_the_composer for the reason.

Wire CDI-managed Beans

The approach to work with CDI is similar to the approach for Spring, except you should register another variable resolver for CDI: DelegatingVariableResolver.

Wiring Sequence

When wiring variables, the predefined sequence to look for variable resolvers is as follows:

  1. The variable resolver defined in the ZUML document.
  2. The variable resolver annotated registered in the class with the VariableResolver annotation.
  3. If none of above is found, it looks for the implicit objects, such as session and page.



Version History

Last Update : 2022/01/12


Version Date Content
6.0.0 April 2012 Initial edition.



Last Update : 2022/01/12

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.