5 Replies Latest reply on Dec 5, 2008 8:33 PM by Francisco Jose Peredo Noguez

    Synchronous @WebRemote call

    Francois Eric Novice

      Hi,


      I am trying to make a synchronous call from my javascript to a backend method.  99% of the time the asynchronous way is fine but in some particular cases, I need the call to be made in a synchronous manner.


      For example, instead of having:


      function test() {
          Seam.Component.getInstance("testAction").getResult("test", callbackMethod);
      }
      
      function callbackMethod(result) {
          alert("RESULT: " + result);
      }
      



      I would like to be able to do:


      function test() {
          var result = Seam.Component.getInstance("testAction").getResult("test");
          alert("RESULT: " + result);
      }
      



      I was able to do it with my previous architecture using Prototype and the AJAXRequest in which I could specify the parameter: asynchronous: false


      Is there some sort of equivalent for Seam Javascript webremote calls?


      Thank you,


      Francois

        • 1. Re: Synchronous @WebRemote call
          Francois Eric Novice

          I have found my answer with this thread:


          http://www.seamframework.org/Community/EJBInjectionInAServlet


          Here is the code snippet that I use:


              remoteCall: function(method, params, callback, isAsync) {
                  var async = (isAsync != null)? isAsync : true;
                  var s = method.split('.');
                  var callParams = [params, callback];
                  var obj = Seam.Component.newInstance(s[0]);
                  Seam.Remoting.async = async;
                  eval('obj.' + s[1] + '.apply(obj, callParams)');
              }
          



          Unfortunately I did need to modify remote.js in order to get this working by adding the parameter for synchronicity:


          Line 602 (added)


          //FRER: Variable that holds state of the request
          Seam.Remoting.async = null;
          



          Line 634-640 (added)


            //FRER: Use async to specify if it is a synchronous or asynchronous request  
            var async = (Seam.Remoting.async != null)? Seam.Remoting.async : true;
            asyncReq.open("POST", Seam.Remoting.resourcePath + path, async);
            
            //FRER: Once it is done reset it to null
            Seam.Remoting.async = null;
          



          Obviously it would be great if a parameter could be added to the method calls with Seam Remoting directly...


          Francois

          • 2. Re: Synchronous @WebRemote call
            Francois Eric Novice

            Hmmm...I just upgraded to versino 2.1.1.CR1 and my patch doesn't work anymore.


            Is there anybody that uses any synchronous requests?  This is essential for me and as I said before asynchronous is almost always correct but I have a few places where I would like to keep it synchronous.


            Anybody been able to do it?


            Any idea what I could do to activate this?


            Thank you

            • 3. Re: Synchronous @WebRemote call
              Francisco Jose Peredo Noguez Master

              Do you realize that with synchronous calls from your javascript to a backend method you basically hang you browser until it gets a response... I can not imagine a case when a user could like that...

              • 4. Re: Synchronous @WebRemote call
                Francois Eric Novice

                Yes I do understand it. I normally don't use it but in some cases I do not want the user to have the flexibility to do anything else...I want him to wait.


                I know this is not a very popular design pattern and I myself do not encourage it. Prototype does allow it though because there are some practical uses for it.


                Would it be possible to activate this for Seam?

                • 5. Re: Synchronous @WebRemote call
                  Francisco Jose Peredo Noguez Master

                  Francois Eric wrote on Dec 05, 2008 15:34:


                  Yes I do understand it. I normally don't use it but in some cases I do not want the user to have the flexibility to do anything else...I want him to wait.



                  Make the user wait, and hanging the browser are very diffent things, if you want to make him wait, use a modal dialog saying: Please wait (You can build it pretty easily by combining a4j:status with rich:modalPanel. It will automatically force the user to wait on ajax requests without hanging the browser.




                  I know this is not a very popular design pattern and I myself do not encourage it. Prototype does allow it though because there are some practical uses for it.



                  Practical uses for hanging the user browser? Can you give me an example?



                  Would it be possible to activate this for Seam?



                  I would consider it a bug if it were possible.