Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    [Simple] Callback confusion with isc.RPCManager.sendRequest

    In the Code Snippet below (as a standalone example) i tried to get a simple answer from a perl(cgi) script which works in the sense that the response_object has the data i want/expect
    As i understood it from several sources the callback function would fire after a successful response.

    When i check the value of "answer" before returning it or the value of "loginStatus" after functest has run its course i recieve "false" in both cases even tough the value recieved and processed in the callback is correct and should set "answer" to true.

    Am i missing something here with the callback/RPC ?? or is the Problem somewhere else ??


    ....
    Code:
    <SCRIPT>var isomorphicDir="../../isomorphic/";</SCRIPT>
        <SCRIPT SRC=../../isomorphic/system/modules/ISC_Core.js></SCRIPT>
        <SCRIPT SRC=../../isomorphic/system/modules/ISC_Foundation.js></SCRIPT>
        <SCRIPT SRC=../../isomorphic/system/modules/ISC_Containers.js></SCRIPT>
        <SCRIPT SRC=../../isomorphic/system/modules/ISC_Grids.js></SCRIPT>
        <SCRIPT SRC=../../isomorphic/system/modules/ISC_Forms.js></SCRIPT>
        <SCRIPT SRC=../../isomorphic/system/modules/ISC_DataBinding.js></SCRIPT>
      <SCRIPT SRC=../../isomorphic/skins/SmartClient/load_skin.js></SCRIPT>
        <SCRIPT SRC=../../isomorphic/locales/frameworkMessages_de.properties></SCRIPT>
    .....
    Code:
    <SCRIPT>
    
    
    isc.Button.create(
    {
        left:20,
    
        title: "Login",
        click : function () {
            isc.showLoginDialog(function (credentials, dialogCallback) {
            
                var loginStatus = functest(credentials.username,credentials.password);
                console.log("LOG "+ loginStatus);
                dialogCallback(loginStatus);
                
            },
            {
            dismissable: true
            });
    },
        top:45
    
    });
    
    function functest (user,pass) {
    
        var answer = false;
        isc.RPCManager.sendRequest({
                    actionURL: "scripts/logcheck.pl",
                    containsCredentials:true,
                    httpMethod: "POST",
                    useSimpleHttp: true,
                    showPrompt:false,
                    serverOutputAsString: true,
                    params: {    j_username: user,
                                j_password: pass},
                    callback: function (RpcResponse_o) {
                        if (RpcResponse_o.data == "1") {
                            answer = true;
                            console.log("change_aw"+ answer);
    
                        }
                    }
        });
        console.log("return_aw"+ answer);
        return answer;
    }
    
    
    </SCRIPT>

    #2
    The callback is executed asynchronously, when the server replies. Your method exits synchronously, so the callback has not been run, and answer will always be false.

    Comment


      #3
      Originally posted by Isomorphic View Post
      The callback is executed asynchronously, when the server replies. Your method exits synchronously, so the callback has not been run, and answer will always be false.

      ok i feel slightly stupid now :)
      but i went over the methods/paramas again and i could not find a way to make calls synchronous like i would be able to in "regular" javascript

      Would the correct way to do this actually be a normal sync request without the methodes provided by the framework or which way would it be intended here with "non-java backend"

      I am going to read over the php and quick start guide again but any help is appriciated

      Comment


        #4
        Only certain browsers allow synchronous server calls in JavaScript and those that do are moving to disallow it, as it's generally seen as a bad practice.

        You should instead arrange your code to expect server contact to be asynchronous.

        Comment


          #5
          Originally posted by Isomorphic View Post
          Only certain browsers allow synchronous server calls in JavaScript and those that do are moving to disallow it, as it's generally seen as a bad practice.

          You should instead arrange your code to expect server contact to be asynchronous.

          hmm very well :)
          in that case i have changed the code to accomodate (for anyone interested see below)

          thank you for the quick help and new information


          in the end i could just give over the callback from the dialog to the new function that handles login to call on it but for prototyping this works just as well
          Code:
          isc.Button.create(
          {
              left:20,
          
              title: "Login",
              click : function () {
                  isc.showLoginDialog(function (credentials, dialogCallback) {
          
                      isc.RPCManager.sendRequest({
                          actionURL: "scripts/logcheck.pl",
                          containsCredentials:true,
                          httpMethod: "POST",
                          useSimpleHttp: true,
                          showPrompt:false,
                          serverOutputAsString: true,
                          params: {    j_username: credentials.username,
                                      j_password: credentials.password},
                          callback: function (RpcResponse_o) {
                              if (RpcResponse_o.data == "1") {
                                  dialogCallback(true);
                              } else {
                                  dialogCallback(false);
                              }
                              
                          }
                          
                      });
                      
                  },
                  {
                  dismissable: true
                  }
              );
              },
              top:45
          
          }
          );


          Comment

          Working...
          X