Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a callback to JSF a4j:jsFunction oncomplete event

I am trying to make call to JSF functions in my app more dynamic. Instead of static way of writing callback functions to oncomplete event by hand, I wish to send a callback function as a parameter and make it's invoke inside oncomplete event of the function. Here's an example:

<script type="text/javascript">
  myFunc('myParamValue', function(){
    doThis();
    andDoThis();
  });
</script>

<a4j:jsFunction name="myFunc" actionListener="#{...}" data="" oncomplete="">
  <f:param name="myParam" />
  <f:param name="callback" />
</a4j:jsFunction>

I wish to ask if that would be possible by using data attribute of a4j:jsFunction? Something like this:

...
data="#{myBean.callback}"
oncomplete="if (typeof window[event.data] == 'function') window[event.data]();"
...
like image 448
Nik Sumeiko Avatar asked Jun 09 '11 09:06

Nik Sumeiko


2 Answers

Try something like this:

// Page

<a4j:jsFunction name="callScript" data="#{bean.someProperty1}" 
        reRender="someComponent" 
        oncomplete="execute(data.callback)">

      <a4j:actionparam name="something" assignTo="#{bean.something}"/>
      <a4j:actionparam name="callback" assignTo="#{bean.callback}"/>

</a4j:jsFunction>

// JS

function testFunction() {
    alert("It works!");
}

function execute(funcName) {
    //is no namespace use window
    window[funcName]();
}

//call
callScript("param1", "testFunction");
like image 75
Tarcísio Júnior Avatar answered Nov 16 '22 03:11

Tarcísio Júnior


Try this:

<a4j:jsFunction name="callScript" data=".." 
    reRender="someComponent" 
    oncomplete="foo(data,request)">

  <a4j:actionparam name="something" assignTo="#{bean.something}"/>
  <a4j:actionparam name="callback" />

and js

function foo(data,request) {
 var callback =request.options.parameters.callback ;
 callback(data); 
 }
like image 39
jsCoder2012 Avatar answered Nov 16 '22 02:11

jsCoder2012