How to correctly access the data field in an a4j:jsFunction result
-
21-09-2019 - |
Question
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?
Solution
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 :)
OTHER TIPS
I have it exactly as your setup and it works for me. The only difference is, I have extra attributes on jsFunction:
<a4j:form>
<a4j:jsFunction name="jsFunc1"
action="#{Bean.shouldBeConfirmed()}"
data="#{Bean.booleanResult}"
oncomplete="alert(data);"
ignoreDupResponses="true"
eventQueue="foo">
...
And my data (your booleanResult) is an int. Works fine.
I have also face the same problem.. In case of Boolean results we need to write full method name instead removing getter... like in your case it should be #{Bean.isBooleanResult}.... Let me know if face problem...