Search code examples
javascriptajaxjsfajax4jsf

javascript var not correctly passed to backing bean


I have this piece of code that fails to work. I have fieldToStore defined. I can alert it and I can see its value in firebug console, but when it is passed to myBean it is always undefined in case of a String value or false in case it's boolean. Any ideas?

<a4j:commandButton id="bt1" action="#{myBean.doSomething}" onclick="fieldToStore=false;saveFieldState();">
</a4j:commandButton>        
<a4j:jsFunction name="saveFieldState" action="#{myBean.dummyAction}" />
   <a4j:actionparam name="fieldToStore" assignTo="#{myBean.fieldToStore}" />
</a4j:jsFunction>

My expectation is that after i click on bt1 the value of myBean.fieldToStore is false, as per the javascript var fieldToStore.


Solution

  • I figured it out... the problem was on the way i was calling the jsFunction on the event onclick:

    onclick="fieldToStore=false;saveFieldState();
    

    it should be:

    onclick="fieldToStore=false;saveFieldState(fieldToStore);
    

    The example on richfaces documentation is misleading:

    <body onload="callScript()">
      <h:form>
             ...
            <a4j:jsFunction name="callScript" data="#{bean.someProperty1}" reRender="someComponent" oncomplete="myScript(data.subProperty1, data.subProperty2)">
                  <a4j:actionparam name="param_name" assignTo="#{bean.someProperty2}"/>
            </a4j:jsFunction>
            ...
      </h:form>
      ...
    </body>
    

    Imo it should be onload="callScript(param_name)".