-
1. Re: How to pass return value of Java call out from <aj4:jsFunction>
mcmurdosound Dec 24, 2011 4:52 AM (in response to jsface)It's actually quite easy. Just replace "action" with "data":
<a4j:jsFunction name="demoFunction" data="#{bean.myMethod()}" oncomplete="alert(data);" />
bean:
public String myMethod{
return "hello world";
}
You'll have to keep in mind that ajax is asynchron. Therefore you'll have to use a callback handler like "oncomplete=alert(data);" in my example.
Even more complex objects are possible. JSON for example.
-
2. Re: How to pass return value of Java call out from <aj4:jsFunction>
feuyeux Dec 27, 2011 10:38 AM (in response to jsface)I don't think it's the right approach to use this tag.
My sample:
http://community.jboss.org/wiki/UseA4jjsFunctionToProxyTheJavascript
For your scenario, the common way is use action to invoke the beakingbean's method, and in this method, some attributes of this bean will be updated, and then render the other component whose value is the updated attribute.
i.e.:
<a4j:commandButtton ... action=#{a.m} render="c"/>
<h:outputText id="c" value=#{a.x}/>
A{
x;
public .. getX(){
return x;
}
public void m(){
x=...;
}
}
-
3. Re: How to pass return value of Java call out from <aj4:jsFunction>
jsface Dec 27, 2011 8:04 PM (in response to feuyeux)Thanks for you guys' reply.
Actually, I need use JavaScript. I thought <a4j:jsFunction> is the best RichFaces component suitable for such case.
However, I couldn't make <a4j:jsFunction name="demoFunction" data="#{bean.myMethod()}" oncomplete="alert(data);" /> work for my scenario. The code looks like this
<script type="text/javascript">
...
var greeting;
function setGreeting(data) {
greeting = data;
}
demoFunction();
// calling demoFunction() is supposed to
// 1. invoke bean.myMethod() on server side and return value back to JavaScript
// 2. and then call setGreeting() and thus set variable greeting indirectly
//
// now this variable
//
if (greeting != undefined) {
doSomething();
}
else {
doSomethingElse();
}
...
</script>
<a4j:jsFunction name="demoFunction" data="#{bean.myMethod()}" oncomplete="setGreeting(data);" />
bean:
public String myMethod{
return "hello world";
}
This jsFunction call looks odd to me too. It is not direct invocation and value passing like
var greeting = demoFunction(); or var greeting = myJsFunction(); in my first post.
JavaScript itself is horrible to debug and test and there seems no clue what this <a4j:jsFunction> plays with its attributies and how <a4j:jsFunction> works with JavaScript.
I gave up by just using the following syntax
<script type="text/javascript">
...
if ("#{bean.myMethod()}" != "") {
doSomething();
}
else {
doSomethingElse();
}
...
</script>
But I really wish <a4j:jsFunction> can pass value back easily.
Thanks again for the reply.
-
4. Re: How to pass return value of Java call out from <aj4:jsFunction>
mcmurdosound Dec 28, 2011 3:46 AM (in response to jsface)As I've already mentioned, jsFunctions are ajax and therefore asynchronous. If you call one of them, the ajax post is made but the control goes staight to the next line of code. You'll have to resume your methods after the result comes back from the backing bean. This can be done with callbackhandlers implemented by "oncomplete". The "data" object contains some serializable data, that can be parsed on the client. The callback handle has now the result and can continue your algorithms / program / procedure whatsoever.
<a4j:jsFunction name="demoFunction" data="#{bean.myMethod()}" oncomplete="setGreeting(data);" />
var greeting;
function setGreeting(data) {
greeting = data;
if (greeting != undefined) {
doSomething();
}
else {
doSomethingElse();
}
...}
-
5. Re: How to pass return value of Java call out from <aj4:jsFunction>
jsface Dec 28, 2011 1:55 PM (in response to mcmurdosound)I gave it another try but the callback handler seems not fired. The page contains a lot of other components. Even worse, the change made the page stops working.
I believe <aj4:jsFunction> works as you claimed. I saw some other forum, questions/answers on other website mentioning it as well. But I guess I have to isolate it to a separate small example and try it again from there. I just need wait until I have extra time.
Thanks a lot again! Happy New Year!
-
6. Re: How to pass return value of Java call out from <aj4:jsFunction>
ricardo.bento Jun 11, 2014 12:16 PM (in response to jsface)I know it is old but for possibile references (event.data):
try with <a4j:jsFunction name="demoFunction" data="#{bean.myMethod()}" oncomplete="setGreeting(event.data);" />