7 Replies Latest reply on Aug 27, 2007 11:27 AM by Mike Maurer

    Call javascript server-side

    Moshe Immerman Newbie

      Is there any way I call javascript methods on the client from a server side managed bean?

        • 1. Re: Call javascript server-side
          Sergey Smirnov Master

          Could you clarify what do you mean? Some usecases will be great

          • 2. Re: Call javascript server-side
            Moshe Immerman Newbie

            In IceFaces I can make any javascript call using JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), 'alert();');

            I use this functionality extensively: popping up dialog boxes, error/sucess message, upatine JS components etc

            • 3. Re: Call javascript server-side
              Sergey Smirnov Master

               

              "moshe@egis-software.com" wrote:
              JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), 'alert();');


              This does not mean calling javascript from the server. It means performing the call on the client side when ajax request is complete.

              The ajax4jsf's equivalent is 'oncomplete' attribute of any ajax command components (a4j:support, a4j:commandButton, a4j:commandLink, a4j:poll, a4j:push, a4j:jsFunction, etc).
              For example:
              <a4j:commandButton value="update" oncomplete="alert('it is me!')" />


              Moreover, ajax command components have a 'data' attribute that allows to pass any data from server to client and use it in the javascript invocation.

              for example:

              <a4j:commandButton value="update" data="#{mbean.customer}" oncomplete="doSomething(data.firstName, data.lastName)" />


              • 4. Re: Call javascript server-side
                Moshe Immerman Newbie

                Is there any way of getting the same functionality without binding to a specific component?

                I need to call JS when the current ajax request (from any component) completes not when the ajax request from a specific component completes.

                For example: in my error handler on the server I need to display an error message on the client.


                What type of integration is there with jMaki?

                • 5. Re: Call javascript server-side
                  Igor Shabalov Apprentice

                  Nice feature, I meen ability to add JS call payload to any ajax response. So far we have only ability to add data payload. We will have it in few weeks. Thank you for request :-)

                  • 6. Re: Call javascript server-side
                    Moshe Immerman Newbie

                    cool - thanks for the quick response

                    • 7. Re: Call javascript server-side
                      Mike Maurer Newbie

                       

                      "SergeySmirnov" wrote:
                      "moshe@egis-software.com" wrote:
                      JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), 'alert();');


                      ...Moreover, ajax command components have a 'data' attribute that allows to pass any data from server to client and use it in the javascript invocation.

                      for example:

                      <a4j:commandButton value="update" data="#{mbean.customer}" oncomplete="doSomething(data.firstName, data.lastName)" />



                      In your example above with the commandButton and the 'data' and 'oncomplete' attributes, what would the java code look like for the getCustomer() method in mbean? What kind of an object is it returning in order to access the data via "data.firstName, data.lastName"? Can that method just return a String and be referenced in the javascript by simply saying doSomething(data)?