jsficefaces

interrupt a Bean-method by calling a javaScript function?


What I want to do:

Details:

Problem:

Is it possible to start the javaScript function by interrupting the bean-function?


EDIT:

Added an example Bean of how I how thought it should work, if it helps:

public class MyBean {

  public String callJavaScriptFunctionAndWaitAndHandleResult() {

    // Call a javascript function from the bean
    callJavaScriptFunction();

    // Waits untill the javascript has returned some stuff
    pause(); // <-- IS IT POSSIBLE TO WAIT HERE AND RUN A JAVASCIPT FUNCTION?

    // Handle the result you got from the javascript function
    handleResultFromJavaScript();
  }
}

Solution

  • Disclaimer: Code in the answer will contain untested code in PrimeFaces flavour and 'pseudo code'. Since I do not use/run IceFaces nor do I have the intention to, I cannot (will not) test the code and will only provide the PrimeFaces counterparts as an (untested) example

    Answer

    You seem to be stuck trying to get a technical solution to work that cannot work. JSF cannot 'pause' things and will only return data at the end of the bean method that is called. You are most likely mislead by the statement in the IcesFaces documentation

    "Send immediately Javascript code to the client for evaluation and execution"

    This cannot work due to the way JSF works and it is sort of not really good English. The developers should have stated something like

    "Send Javascript code to the client at the end of the method call for immediate evaluation and execution"

    The immediate means that it is not some <script> .... </script> that gets added to the page and that can be executed again and again by e.g. calling a function that is defined in the script. It does not mean you cannot call existing javasript functions (ones that are already in your DOM) from the returned script (effectively that is what we will be using)

    So how to solve your problem. Start by breaking the method down in two, lets call them step1 and step2

    void step1() {
    
        // Do things
    
        // return javascript
    
    }
    
    void step2() {
    
        // Do other things using values that will be posted from the client by javascript
    
    }
    

    How to return javascript/call javascript from a bean is an existing Q/A here in Stackoverflow. For the IcesFaces counterpart I refer to their documentation. A PrimeFaces example is

    PrimeFaces.current().executeScript("alert('This onload script is added from backing bean.'); clientSideFunction();");
    

    But at the end of the javascript you want to call a server side method in a bean from javascript. The opposite of what you need in step one.

    A PrimeFaces example being

    <p:remoteCommand name="clientSideFunction" action="#{bean.step2}" />
    

    And in since in calling step2() you want to pass parameters to the bean, you need to implement this as well.

    clientSideFunction([{name:'x', value:10}, {name:'y', value:20}]);
    

    You can use javascript variables in there (e.g. a complete json string in a variable) and need to process these variables (especially see the 'update' at the end)