Skip to Content
0

PERSONAS : Best practice to reuse script function?

Apr 06 at 10:17 AM

106

avatar image

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Pierre Jacquot May 18 at 07:31 AM
0

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

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Ok "...executeScript.." was the correct call. I don't know why i was stubborn with the "...getScriptById..."

Merci pour ton aide!

Alexandre

0
Cristiano Hansen
Apr 06 at 12:24 PM
0

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

Show 3 Share
10 |10000 characters needed characters left characters exceeded

Thanks Cris for your time and your answer.

I've oriented the question in a way where i want to speak only about performance. I understand your logic as i already have a global.js file which i'm using through several personas screen but again, not my point in this post, i just want to know the difference in term of performance of the commands above (slower, faster, same?)

A colleague of mine encoutered perfomance issues with option 1. When going with global ressource (option 2) he told me it was faster. So i'd like to know if it's true or if it is just a combination of factors in his project .

0

Hi Alexandre,

I have discussed this with my colleagues.
From a performance perspective, it is best to use a global script and define a library/function that you can reuse.
When you load the global script, you can define that the script should only be loaded once and will be reused for follow up calls.

An example:
Script 1: session.utils.log('script_1');
Script 2: session.utils.log('script_2');
session.utils.executeScript("wnd[0]/scrptPersonas_005056BB59E71ED88F831ED1E7937D7E", null); // execute script 1
Script 3:

/*globals myLogFn */

session.utils.include("005056BB59E71ED88F8351BD6A5F1DB7", false);
myLogFn('script_3');

with the global script being:
session.utils.log('global script loaded');
myLogFn = function(sMessage) {
session.utils.log(sMessage);
};

Whenever you execute script 2, it will need one roundtrip more than script 1, because it needs to fetch script 1 from the backend.
When you execute script 3, it will have an additional roundtrip the first time it is executed, but from then on, it will need the same number of roundtrips as script 1.

I hope this clarifies,
Cris

1

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

0
Alexandre Ourth Apr 11 at 08:45 AM
0

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

Share
10 |10000 characters needed characters left characters exceeded