Skip to Content

PERSONAS : Best practice to reuse script function?

Hi,

Let's say i have a functionality useful_func in a script 'SCRIPT_1' that i want to reuse in another script, 'SCRIPT_2' of the same flavor.

Which option is better for performance ?

1 -

//Inside SCRIPT_2

var myScript = session.utils.getScriptById("SCRIPT_1");
myScript.useful_func();

2 - Using a global javascript ressource

//Inside SCRIPT_1 and SCRIPT_2

globalLibrary = session.utils.include("<GUID>", true);
globalLibrary.useful_func();

Edit :

3 -

//Inside SCRIPT_1
globalLibrary = session.utils.include("<GUID>", true);
globalLibrary.useful_func();
// SCRIPT_2
var myScript = session.utils.getScriptById("SCRIPT_1");
myScript.globalLibrary.useful_func();

Regards,

Alexandre

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    May 18 at 07:31 AM

    Hi Alexander,

    may be this solution can help you :

    I created a "common" script in my flavor : "CommonFunctions"

    I can see the internal script name in the blue line just over the script : Conditionnement_03 - ZPP_UM_OF - 0100 - wnd[0]/scrptPersonas_6

    Script definition :

    RefreshPrintButtonsX = function()
    {
    //Mise en forme des boutons d'imprimantes
    //Identification et désactivation des boutons
    var Button1 = "wnd[0]/usr/btnPersonas_148360205268814";
    session.findById(Button1).setProperty("fontColor", "rgba(221,221,221,1)");
    .....

    Now I'm using this script by calling it from other scripts with this instruction : executeScript

    //Mise en forme du bouton d'imprimante
    session.utils.executeScript("wnd[0]/scrptPersonas_6");
    var PrintButtons = RefreshPrintButtonsX();
    .......

    Best regards,

    Pierre

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 06 at 12:24 PM

    Hi Alexandre,

    If you intend to use the same scripts somewhere else, i.e. in another flavor, then it would make sense to have them in a global javascript resource.
    If you intend to use in the same flavor, then using the getScriptById should be sufficient.

    Regards,
    Cris

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Cris,

      I didn't get any notification for your comment, sorry for replying late. I think i understand :) It's better to use a global ressource with the "False" attribute (load only one time in a session). For now on i was using the "Yes" cause i didn't succeeded to launch my scripts. I've tried a little bit more and now it seems to work.

      Last question :

      //Global.js ressource
       myLogFn = function(sMessage) {
       session.utils.log(sMessage);
      };

      Which option is better?

      //SCRIPT_1 in flavor
      session.utils.include("global.js GUID",false);
      myLogFn('Script1');
      
      //SCRIPT_2 in flavor
      session.utils.include("global.js GUID",false);
      myLogFn('Script2');
      
      
      => We include the global.JS in EACH script

      Or

      //SCRIPT_1 in flavor
      session.utils.include("global.js GUID",false);
      myLogFn('Script1');
      
      //SCRIPT_2 in flavor
      var script = session.utils.getScriptById('SCRIPT_1');
      script.myLogFn('Script2'); //I suppose this syntax works
      
      
      => We include the global.JS ONLY  in SCRIPT_1, and then we call it in SCRIPT_2 using SCRIPT_1 reference

  • Apr 11 at 08:45 AM

    To sum up : It's better to use global ressource => OK. Now, if i want to call methods of my global ressource into several scripts of my flavor, what is the best way to do that?

    1 ) I call the "session.utils.include" command in each script

    2 ) Or, i call the "session.utils.include" command in one of my script (like in a "TOP_SCRIPT"), and then, within all my other scripts, i call "TOP_SCRIPT.my_function();"

    3 ) Performances for 1 & 2 are the same

    -----------------------------------------------------------------------

    EDIT : It seems option 2 is not working sadly...

     
    //----------------------------
    //  WORKING
    //----------------------------
    //session.utils.include("00505699466B1ED88FAD397B237EB234", false);
    //var notifNo = session.findById("wnd[0]/usr/ctxtPersonas_152086760259773");
    //goToServiceNotif(notifNo.text,"IW53");
    
    //----------------------------
    //  NOT WORKING :(
    //----------------------------
    var script = session.utils.getScriptById('wnd[0]/scrptPersonas_00505699466B1EE88F98CE878BE76FD4');
    var notifNo = session.findById("wnd[0]/usr/ctxtPersonas_152086760259773");
    script.goToServiceNotif(notifNo.text,"IW53");
    
    
    // => Is my syntax wrong???? <= Did i missed something somewhere or is it something we cannot do with personas?

    Regards,

    Alexandre

    Add comment
    10|10000 characters needed characters exceeded