This script works just fine for a button that is visible on the main login page of the application. However, I want to add this to our nav menu after the user logs in. To do this, I have written this in a project library script:
I want to call this project library script as part of another script that runs when the right component is clicked on, but when I test the script in the script console as "translate.en_to_es()", I get the following error:
Traceback (most recent call last):
File "", line 1, in
File "module:translate", line 2, in en_to_es
NameError: global name 'self' is not defined
When I make a test script with a print function and call it in the same way, it works fine.
Am I correct that is has to do with the way it's calling the locale prop? It's not on a button so self.session probably can't reach it. But what is the correct way to modify that prop from the project library?
self refers to the container or component in Perspective. The gateway doesn't know what you're referring to and also doesn't know what (who's browser) session you're referring to (and if you got it to work it would affect everyone's session).
A simpler solution is to use a Radio Group or a dropdown.
Worth calling out as well - the script console is running your code in the local designer scope, not on the gateway, as scripts in Perspective will be (even the Perspective session in your designer, for consistency).
And the project library is just that - a library, that you can call into from anywhere, but it doesn't have any inherent scope attached; project library code may or may not work when called from the gateway or the designer, depending on what it's actually doing.
So the reason I was trying to use a callable script is because I’m using this menu from the Exchange:
When you dig into it down to the bottom, there’s a script that runs on click on the container. That script is just a navigate script based on a parameter that you set at the top level of the three views.
I want to keep the existing navigation function, so I created a new BOOL parameter that passes through to the script, so I can toggle it if I want it to perform another function on click. I was going to try to call the specific script with another parameter that passes the name of the translate script into the ‘on click’ script.
The idea being that for each element of the flex repeater on the top level, I can dig into and set the parameters to perform the standard navigate function or to perform some other function when it’s clicked.
Change this to def en_to_es(self): and then include self from the point where you call it.
Project library scripts have no scope of their own, and generally have no context (not quite true). If a variable is passed into an event, and you want it within something you call, pass it inward.
That's good to know. I'm completely knew to both Python and Javascript and really anything that's going on in Perspective so this is all good info for me. Thank you!
That is a great solution and I may use that in the future, but for this project specifically, I'm trying to get it to work within an existing script that navigates to a page. It's a flex view within a view that's called in a flex repeater, and I added parameters to toggle between the navigate function and any other script that I want to run 'on click.'
Are you trying to dynamically assign what script to run on the button press event by trying to specify the script you want to run as a parameter? If so, that will not work. What are you trying to accomplish/simplify with this?
If you are trying to be able to dynamically tell the button what to do, a better way would be use a typeId property/param and come up with some set values for it. Then, in a top level script, you compare the passed value of typeId with known values and if it matches the value, run the specific script you decided to associate with that value.