How to correctly access the data field in an a4j:jsFunction result

Ben picture Ben · Sep 2, 2009 · Viewed 12.6k times · Source

I am currently trying to validate some front-end values with some server-side methods (as a group) and am running into issues dealing with the result.

Here is the XHTML for the button that starts it all:

<h:commandButton action="#{Bean.save()}" 
        value="Save" 
           id="save" 
      onclick="checkForConfirmation();" />

And the javascript, part of which the button calls, the other part of which the jsFunction calls

function checkForConfirmation()
{
         var name = document.getElementById("path:to:name").value;
         var address = document.getElementById("path:to:address").value;
         var city = document.getElementById("path:to:city").value;
         var state = document.getElementById("path:to:state").value;
         var zip = document.getElementById("path:to:zip").value;

         jsFunc1(name, address, city, state, zip);
}
function showConfirmPrompt()
{
            if(confirm('Confirmation before save')) 
            {
                return true;
            }

            return false; 
}

And finally, the jsFunction which is the problematic piece:

<a4j:form>
        <a4j:jsFunction name="jsFunc1" action="#{Bean.shouldBeConfirmed()}" data="#{Bean.booleanResult}" oncomplete="alert(data); if (data) {showConfirmPrompt();}">
            <a4j:actionparam name="param1" assignTo="#{Bean.nameToBeValidated}"/>
            <a4j:actionparam name="param2" assignTo="#{Bean.addressToBeValidated}"/>
            <a4j:actionparam name="param3" assignTo="#{Bean.cityToBeValidated}"/>
            <a4j:actionparam name="param4" assignTo="#{Bean.stateToBeValidated}"/>
            <a4j:actionparam name="param5" assignTo="#{Bean.zipToBeValidated}"/>
        </a4j:jsFunction>
    </a4j:form>

The problem is that, towards the end of this chain of events, the alert(data) in the 'oncomplete' attribute shows that the data is undefined. I need this to be defined in order to know whether or not to show a warning dialogue.

I can confirm that the Bean.shouldBeConfirmed() method is indeed running, and with the right parameters, and indeed returning the correct value, and even setting the value of the Bean.booleanResult variable (which is a normal java boolean). What am I doing wrong here?

Answer

Niks picture Niks · Feb 24, 2011

Though this is a very old question, I had the same issue with Richfaces 4 M3. Parameteres suggested by Yev didn't work. So referred to richfaces's JIRA. According to which, it is a bug (which is in "won't fix" mode, don't know why!) So for now the workaround is to use event.data instead of data in the oncomplete handler. I tested and it does work :)