Facility Control

 View Only
  • 1.  ogscript.asyncPost - How to pass on variable?

    Posted 06-02-2018 09:08

    So... I might just be stupid here, or have a real brainfart for that matter...

    The following code is just to grab the reponse from a website:

    function testThis(resultData) {
       if (resultData == null) {
          ogscript.debug("NO DATA");
       } else {    
          ogscript.debug(resultData);
       }
    }
    
    var url = 'SOME URL HERE';
    ogscript.asyncPost(url, null, testThis);

    How would I go about passing a variable into the "testThis" function?
    I know about passing variables into functions... But resultData is not defined anywhere but in the testThis function, and I assume that causes the issue?
    Ref W3Schools page on JS Function Parameters: https://www.w3schools.com/js/js_function_parameters.asp

    I was thinking along the lines of...

    function testThis(resultData, test) {
       if (resultData == null) {
          ogscript.debug("NO DATA");
          ogscript.debug(test);
       } else {    
          ogscript.debug(resultData);
          ogscript.debug(test);
       }
    }
    
    var val = 'TESTING';
    var url = 'SOME URL HERE';
    ogscript.asyncPost(url, null, testThis(val));

    EDIT: Have tried different variations, and different ways of doing this now, but all of them seems to break the code...



  • 2.  RE: ogscript.asyncPost - How to pass on variable?

    Posted 06-02-2018 22:57
    Ok, so defining the variable after the function and not define it as a variable in the "testThis" function, and before the ogscript.asyncPost function appereantly works.

    I thought that would be out of scope, but I guess not. interesting...
    However that means I cannot place the function called by the ogscript.asyncPost into the API of the DashBoard, as it wont be declared as a global variable. I guess I could store the value in a temp parameter, but that just seems excessive...

    Ideas?
    #DashBoard


  • 3.  RE: ogscript.asyncPost - How to pass on variable?

    Posted 06-04-2018 14:23

    The signatures for the callback functions are fixed - so you can't add your own arguments to those.

    Your callback function can see any variables that are in their parent scope (or their parent's parent, etc.) so, that is one way to see additional data (as you discovered). If you're using a global variable, one downside would be that you can only have one copy of those variable values at a time.

    Another mechanism would be to have a 'function factory' that can be called to create your callback functions with your extra variables:

    function createCallback(extraVar1, extraVar2)
    {
       return function(resultData) {
         ogscript.debug('You got ' + resultData + ' and your extra variables were ' + extraVar1 + ' ' + extraVar2);
       }
    }
    
    ogscript.asyncPost('http://www.rossvideo.com', null, createCallback('Hello', 'World'));

    Another option may be helpful depending on what the data you're looking to use in your callback. You can call
    ogscript.asyncPost(URL, DATA, CALLBACK, INCLUDE_RESPONSE); Where INCLUDE_RESPONSE is true or false. If true, resultData contains more information than just the text from the URL.

    You'll have:

    resultData.responseCode; //THE HTTP response code (generally 200)
    resultData.url; //The URL the response is for
    resultData.contentType; //The content type of the response
    resultData.bytes; //If the returned content is binary
    resultData.value; //The original returned string

    #DashBoard