Can JavaScript functions be defined for use in Servoy? If so, I’ve not yet discovered appropriate syntax?
Servoy objects if the “function” keyword appears within a method. Servoy’s parser will verify lines such as “new x();” but when run it will say that “x” has not been defined.
Can someone provide some clarification and/or useful references? I’m working from O’Reilly’s JavaScript The Definitive Guide.
jcompagner:
no you can’t define function in youre scripts.
The methods you are making are functions. So if you want a function then you must make a script/method.
so if you want to tryout things of javascript in that book. Everywhere where it makes a function you should create a method.
Here’s my problem. I need to copy some field values from one table to another. Variables live only within the current method. I need to pick up some field values and get them into equivalent fields of another table. See my posting “Scripting many to many”.
Specifically I’m building a join file between the company and people tables. I need to populate the join table with the company_id and people_id field values.
I’m sure as I move on I’ll have many other instances where values need to moved from table to table, passed from method to method. In FMP I’m used to using local “globals” when using a constant to establish relationships between tables. Servoy has a radically different structure that doesn’t seem to lend itself to this strategy. Haven’t yet seen anything in the docs that outlines a useful strategy for this purpose.
Your comments above explain why the word “function” doesn’t appear in any indexes of the documentation. I’ve now become fuzzy about the distinctions between the terms method, script and function.
var test = ‘abc’;
some_method(test); // this will call some_method and pass test
in some_method you do
var test = arguments[0]; // this will deliver the first parameter passed (here: test)
you can pass many parameters: some_method(a, b, c, d, e, …). An arguments array gives you access to these parameters.
Another remark: variables are not always local inside a method. If you define a variable like test = ‘abc’ (without “var”), you can access this variable anywhere.
Yikes! That did it! I’ve been sitting here boxed in by this problem for two days, unable to move forward.
You’ve actually shown me two quite different methods of accomplishing the same thing. I’ve tested both of them and they both accomplish this task.
To just declare a variable without “var” seems so simple. Dumb question: why would anyone need the more complex structure of arrays when persistent variables are available?
Second important question: for safety sake I would imagine such a variable should be set empty or even better “destroyed” in some manner so that some later declaration elsewhere such as company_name = c; won’t produce unexpected results. Or am I being overly concerned?
Your assistance is very much appreciated. So very pleased the solution is so simple.
When you declare the variable without ‘var’ it is a FORM variable and not available elsewhere. But you MUST set it to whatever value when you start using your form or record. I do so, if I use this via onRecordSelect and onShow. If you don’t do so you will recieve an error message.
When you declare the variable without ‘var’ it is a FORM variable and not available elsewhere. But you MUST set it to whatever value when you start using your form or record. I do so, if I use this via onRecordSelect and onShow. If you don’t do so you will recieve an error message.
That’s strange. In one form and table I set c = company_id and p = people_id, then called another method on a different table and successfully set company_id = c and people_id = p. True I didn’t explicitly switch forms, just called from one method to another.
patrick:
as far as I understood this behaviour, “global variables” of this sort are global throughout the solution…
If not, could someone who knows please clarify?
Yes please, I’d like to know about the possible downsides to using global or persistent variables before I start using them liberally. Plus a nudge towards the documentation team to prepare some guidance in this area. When is the array technique (above) preferable, this sort of thing.
you can create variables on most scopes where you want.
If you want global vars but not make them servoy globals.
Do put them in the global scope.
for example you can do this just fine
globals.xxx = yyyy
and xxx is not declared or made as a global dataprovider!
So you can declare variables where ever you want! Some exceptions are there (like elements that one is locked)
jcompagner:
you can create variables on most scopes where you want.
If you want global vars but not make them servoy globals.
Do put them in the global scope.
for example you can do this just fine
globals.xxx = yyyy
and xxx is not declared or made as a global dataprovider!
So you can declare variables where ever you want! Some exceptions are there (like elements that one is locked)
Is there a difference between
xxx = yyyy; // a variable with life beyond the immediate method
and
globals.xxxx = yyyy;
?
Two ways of doing the same thing? Or are there functional differences between the two, and if so, what?
jcompagner:
you can create variables on most scopes where you want.
If you want global vars but not make them servoy globals.
Do put them in the global scope.
for example you can do this just fine
globals.xxx = yyyy
and xxx is not declared or made as a global dataprovider!
So you can declare variables where ever you want! Some exceptions are there (like elements that one is locked)
Is there a difference between
xxx = yyyy; // a variable with life beyond the immediate method
and
globals.xxxx = yyyy;
?
Two ways of doing the same thing? Or are there functional differences between the two, and if so, what?
The difference is that globals.xxx is the supported way of doing it.
In all cases use globals only when you really need to not to store everything in it as they consume memory and tend to get their own unorganized life.
By passing variables from methods your solution is much easier to read and maintain.