Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Personas 3.0: What are the scopes of variable and function in script?

Former Member
0 Kudos

Hi Personas 3.0 experts,'

Scripts are used to make straight forward Ui manipulations normally. But I am thinking a bit complex situations - can we create some "lib" functions inside one script and call these functions in other scripts?

If so, how can these functions be defined "globally", and how can the parameters be passed among functions?

This basically is related to the questions how the scopes of varaibles and functions are defined in scripts?

Consider these cases:

1:  If a var, myVar, is defined "globally (no 'var')" in one script, it can be accessed in other script. - it's clear.

2: Q: How to define a function with parameters in a script that can be called in another script with the parameters?

3: Q: How to pass a variable from one script code to another script code?

Br,

Dong Zhu

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Back to this post question - can I understand that personas script has a scope like a js function for vars (as vars defined in separate scripts are not visible to eaach other)? But for fucntions defined in separate scripts, they are not visible, why (they should be visible if they are under the same scope of "session")?

Thanks.

Dong Zhu

22 REPLIES 22

0 Kudos

Hi Sushant,

Thanks a lot for the links. I have questions that hope you can help:

1. For the Table control in Personas, why don't we offer built-in functions such as:

    oTable.getRoww(rowStart, rowEnd);    //if rowEnd is "END" (or whatever name), it means to the end

    oTable.getColumns(colStart, colEnd);

    oTable.getMatrix(rowStart, rowEnd, colStart, colEnd);

    ...

    Theses are the frequently needed features. I can see as in Steve's post that developers can write script to fetch data from the table control, but it also means everyone has to do the same duplicated JS coding. If so, why don't make it as "standard libs" built-in Personas? It will save every body's time.

I like the "Copy Table" feature in Personas 2.0, but the only drawback is that it doesn't allow copy partial table - it means that for a large table (which is very common in our business) the copying time is too long in order to copy all data which is not necessary. So copy table featur must allow the ranges of rows and columns.

2. If Personas tool can be like some other tools, such as Matlab, where developers can easily add-on customised functions that can be shared with other develoeprs and served as "standard lib funcs", it will really make Personas EASY to use for developments.

3. One of my concern to adding more lib functions is the UI performances. If we add such kind of functions in scripts or make them as the uploaded JS files, what are the UI performance differences?  My understanding is the more scripts, the longer loading time. For our users, it will not be acceptable if the loading time is too long. I need to evaluate different implementation solutions of using JS.

Just some of my thoughts that hopeful to improve the Personas usabilities. 🙂

Br,


Dong Zhu

0 Kudos

Hi Sushant,

The pic below is screen shot of your ref doc.

Can you clarify if the text "all GUIs" marked by red circle include "SAP GUI for HTML" or you just mean SAP GUI for Windows and SAP GUI forJava? I tested in HTML but it does not work for html (I tried to put it inside an immediate function etc). For html the .js file included does not have a "function" scope. The script created inside Personas has a scope like a function.

What I was looking for is a .js file that can be used as a lib for HTML, Windows and Java. Would such a lib be possible? Thx.

Dong Zhu 

0 Kudos

Hi Dong,

1. These can already be achievable through the methods available now. These standard methods for all controls are negotiated and agreed upon by all GUIs so that your flavor works seamless. But of course, you can file a message with us with new dev request. Based on the dev requests we get which might be commonly demanded, we take decisions on that.

2. And thats why we have the concept of global JS libraries, link which I already posted above. Your developers can create some common methods and include those in their scripts.

3. End of day, you can so easily create a very badly designed flavor (including scripting). SO of course, it is also flavor creators job to remove all the unwanted/redundant pieces. From global scripting point of view, you can use the second parameter "alwaysExecute" based on your script design.

Regards,

Sushant

0 Kudos

For the global scripts to work on all GUI (SAP GUI for html/windows/java), as mentioned in the article, there is a small modification you need to make (snapshot you have posted above).

I think what "lib" you are mentioning is the global script. I don't see the difference here based on how will it be used. Did i misunderstand you?

Regards,

Sushant

0 Kudos

Yes, what I am trying is to add such a "global" function/method that can be called by utils in html/wondows/java.

Can you show me how to create such as function? Just take a example function that simply return a string. Thanks.

Dong

0 Kudos

Table copying is exactly one of the things I've used libraries to provide. While there's a great Wiki article describing how to do it, it isn't the sort of thing I'd want less experienced developers building, even if it is just copy and paste. Mistakes happen even then! I'd rather do it once myself, and do it right. And at least if I don't do it right I have only one place to correct it!

I haven't had a need so far to copy just part of a table, but that sounds like something I'd want to add at some point. I have added some functions for building UI5 tables from JavaScript arrays, though, since that is something we're looking at doing a lot (until Personas has native table support, anyway:-)

As for performance, you are right that loading lots of extra code is going to impact performance. I don't have a feel for how much as I don't have any large libraries. I haven't really noticed an extra load time. But if it becomes a problem, you just need to divide your libraries a little so that if you aren't writing scripts that manipulate tables, you don't load the table library.

0 Kudos

In your "library" - the JavaScript file to create as a resource, you have something like this:


var myLib = {

     hello: function() {

          return "Hello from myLib";

     }

}

return myLib;

You upload this as a resource as described in the wiki page Sushant mentioned. Then in your script you include the relevant resource and can call the function like this (substitute your own resource ID, obviously):


session.utils.include("D7825D56FAA91501E100000089CDA770", true);

var text = myLib.hello();

This should work in the HTML gui and Windows/Java guis (once those guis have complete Personas support, which they currently don't).

Steve.

0 Kudos

Hi Steve,

Thanks for the reply. I tried the idea according to teh doc and now I tried your code too. Don'y know if you got error but I got the following error - any idea?

Br,

Dong

0 Kudos

Seems for this "return" syntax, the engine cannot understand it or there is a bug?

Dong

0 Kudos

Well, I mistyped. According to the Wiki page I should of course have said:


myLib = session.utils.include("D7825D56FAA91501E100000089CDA770", true);

var text = myLib.hello();

However, that appears not to work in the browser and I can't currently test it in the Windows GUI (my primary machine is a Mac:-)

What does work in the browser (and I tested it this time...) is this in the include (i.e. create the library object as a global object:


myLib = {

     hello: function() {

          return "Hello from myLib";

     }

}

and this to use it (the mistyped code from my previous reply):


session.utils.include("D7825D56FAA91501E100000089CDA770", true);

var text = myLib.hello();

I suspect the reason the "proper" version doesn't work is due to a bug. The return value from the include is getting lost in the chain of function calls involved in the "include" function, so that the "myLib" object in the calling script ends up with an undefined value.

None of this explains your syntax error, though. I don't get that at all...

0 Kudos

While, I got the same results as yours in all my trials. As long as there is a "return" in the file, the HTML will show the error (complain the missing ';'). The ideas of using global function or object work fine in html, but since they don't have return in the file, they won't work in window and java.

Perhaps SAP can inverstigate what is the right way that is claimed to be able to work for all html/win/java. So far I can't find out such a one.

Thx.

Br,


Dong

0 Kudos

hi dong,

the second option given in the artcile might not work as we are working on a bug..should be fixed in next scheduled release.

meanwhile, the 1st option works.

Sushant

0 Kudos

Hi,

I think this might work for ALL cases HTML/WIN/JAVA as a lib func:

Util=(function(){

   function myLoc(x){/*my actions*/}

   return {

        getValue:myLoc
   };

})();

In Personas script do:

var myValue=Util.getValue(x);

It worked OK for HTML. Since this has a return, it should work for Win/Java too but I did not test it. Perhaps you can test it and fix the info in the guide document.

Dong Zhu

Former Member
0 Kudos

Something to bear in mind when investigating this subject is that scripts in the HTML gui work differently from scripts in the Windows guis. In particular, in the Windows guis the scripting environment disappears completely between script invocations. There are no globals that persist between scripts. The only way to pass values between scripts is to use the put and get methods that Sushant mentions above. And the only way to define functions that are callable by multiple scripts is to use the global libraries technique also mentioned above.

If you restrict yourself to writing code that only works in a browser then you can do a lot more. I quite often create global objects holding both data and methods, created by an onLoad script and containing the bulk of the code for a flavour. Individual event scripts for buttons, etc. then reduce to method calls to this global object. Keeping (almost) all of the code in one place makes for easier maintenance, but this technique only works in the HTML gui.

Steve.

0 Kudos

Hi Steve,

Thanks for the comments.

My opinion is to avoid using global vars which may spoil the namespace as well as introducing restrictions (such as some only work for HTML).

For the lib way, seen in the doc, there can be a "general way" of implementing the "lib funcs" with JS. Instead of using a global method, this may be a good way (as said it works for all envs):

In the myLib.js (which should be uploaded)

var libUtils={

     libMethod1: function(p){ /*body: general-use function*/ },

     libMethod2: function(p){ /*body*/}

};

return libUtils;

So in Personas JS code:

var myUtil=session.utils.include("id-of-libUtils", true);

var myResult=myUtil.libMethod1(myParam);

As said this will work for all platforms. Then it is really a matter to add some "matured" lib functions that can be used by many people.

What do you feel about this?

Br,

DOng Zhu

0 Kudos

I'm all for sharing useful code to prevent people from having to reinvent the wheel. If you have useful functions you use in scripts across several flavours, I'd encourage you to post them here. Maybe when we have a few of them we can consider assembling an "SCN library", and perhaps putting a Wiki page here about it - Personas Knowledge Base - SCN Wiki.

Or maybe SAP might consider adopting some of them into the standard product...

Steve.

0 Kudos

Fully agree! SAP, Personas developers and the tool itself will all get benefit of it. I am aware of this along my way of development.

Just adding a point - for becoming the "lib functions", the codes (of course first have no bugs) have to offer the best perofrmance efficiency - SPEED.

Br,


Dong

Former Member
0 Kudos

Hi,

Back to this post question - can I understand that personas script has a scope like a js function for vars (as vars defined in separate scripts are not visible to eaach other)? But for fucntions defined in separate scripts, they are not visible, why (they should be visible if they are under the same scope of "session")?

Thanks.

Dong Zhu

0 Kudos

A Personas script is essentially an anonymous function - you can see that in the browser debugger. So I would expect to get exactly the same behaviour in a Personas script as you would in a JavaScript function. And if you define a function inside another function, like this


function a()

{

     function b()

     {

     }

}

then function b is not global. It is accessible only inside the body of function a. However, if you assign that function to a variable:


function a()

{

     function b()

     {

     }

     c = b;

}

then c is global, right? You can then call b via c, and b will have access to all of the variables defined inside a since this process creates a "closure".

Does that make sense?

Steve.

0 Kudos

Hi Steve,

Thanks for the clarification. Just one thing - It should be:

c=b;

In order to call c from function b().

Anyway, set yours to correct plus my minor correction.

Br,


Dong

0 Kudos

Oops, you are correct. I've fixed it to avoid confusion. That leaves your comment hanging a little, sorry. But I think it is better this way...

Steve.