Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-2843

MVVM does not support WireVariable rewireOnActivate

    Details

    • gh.sprint.customfield.default.name:
      ZK 8.0.1

      Description

      using @WireVariable(value="testService", rewireOnActivate=true) in a SelectorComposer works as expected rewiring the variable (e.g. a spring bean). After session activation the the variable is resolved and wired again (e.g. after a server restart)

      @VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
      public class SpringTestComposer extends SelectorComposer<Component> {
      	private static final long serialVersionUID = 1L;
      	@WireVariable(value="testService", rewireOnActivate=true)
      	private transient TestService testService;
      

      When trying the same in an MVVM ViewModel the variable remains null after session activation.

      @VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
      public class SpringTestViewModel implements Serializable {
      	private static final long serialVersionUID = 1L;
      	@WireVariable(value = "testService", rewireOnActivate = true)
      	private transient TestService testService;
      

      The reason is BindComposer does not listen to session activation leaving the variable unwired (null).

      A workaround is to extend BindComposer adding this function:

      package org.zkoss.zk.ui.select; //needs to be this package
      import org.zkoss.bind.BindComposer;
      import org.zkoss.zk.ui.Component;
      import org.zkoss.zk.ui.select.Selectors;
      import org.zkoss.zk.ui.util.ComponentActivationListener;
      public class ActivatingBindComposer<T extends Component> extends BindComposer<T> implements ComponentActivationListener{
      	private static final long serialVersionUID = 1L;
      	public void didActivate(Component comp) {
      		Selectors.rewireVariablesOnActivate(comp, this.getViewModel(), 
      			Selectors.newVariableResolvers(this.getViewModel().getClass(), null));
      	}
      	public void willPassivate(Component arg0) {
      	}
      }
      

      then in the zul file the ActivatingBindComposer can be used instead of the default BindComposer

      	<div apply="org.zkoss.zk.ui.select.ActivatingBindComposer"
      		 viewModel="@id('vm') @init('zk.support.spring.SpringTestViewModel')" >
      

        Activity

        Hide
        jumperchen jumperchen added a comment -

        Bug fixed since 8/31/2015

        Show
        jumperchen jumperchen added a comment - Bug fixed since 8/31/2015

          People

          • Assignee:
            jumperchen jumperchen
            Reporter:
            cor3000 cor3000
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - Not Specified
              Not Specified
              Remaining:
              Remaining Estimate - 0 minutes
              0m
              Logged:
              Time Spent - 1 hour
              1h

                Agile