Understanding Scopes
Thank you for joining me for this video. For Servoy University, I’m Bob Cusick. In this video, we’re going to take a look at understanding scripting scopes. There are a couple of different scripting scopes within servoy. We have the solution level or top level scope, which you can see right here. By default, one is created called globals, but you can either delete that or change a name of it. It doesn’t have to be called globals. And those global scopes can contain variables and relations. The next layer is at the form layer. So the form layers have their own scope. So I’m my little people form that I have here. Notice that there’s a variable scope and a relation scope within the form itself. The form level scope are tied to columns and databases and the tables within those databases. You also have what’s called a found set scope. A found set is an object that’s in memory that contains all of the records that you’re currently looking at. So a found set scope has its own properties and methods as well. And to see what they are, if you scroll down here down to database manager and you look at a JS found set object, these are all of the things the JS found set knows how to do. So that’s within the scope of the found set. All right, so let’s take a look at what that actually means in real life. So I’m going to come back up over here to my people form and within the scope of this form, I’m going to create a new variable. I’m going to create a variable called form. Form variable people. I’ll just make it a text field. So this is a form based variable. And it’s just going to be a plain text variable and I can actually go ahead and add that variable to my form here. So I’m going to take this balance to and I’m going to control shift copy it. And we’ll call this variable and I can click it and set the data provider to a form variable people. All right, so the data that goes into this variable only lives within the scope of this form. So it’s accessible to this form. Now we’ll go up to the global’s contact or the solution level context and we’ll make another variable there. So I’m going to call this one global text variable and it’s also going to be a text. Okay, so at the global’s JavaScript object, it has this one variable. You’re probably asking yourself, well, why the heck do you variables in two different places? It’s all about code organization and best practices. But when you’re developing a form, what you want to do is you want to keep all of the logic that specific to that one form within the form scope because other objects that don’t share any of that code don’t need to have any of that code. So it makes the most sense to put it on the form level. If you do have functions or methods that are going to be used by multiple different forms then the best place to put that is up in the global scope. And again, you don’t have to call it global’s you can call it whatever you want. I can even create another scope. Let’s create another global scope and we’ll call this scope Bob Scope. So Bob Scope works just like the global scope. I can create variables. Bob Scope. Variable. All right. So the difference comes in how you access these different scopes. I’m going to go ahead here and I’m just going to make a button that does nothing and I’ll add a new method on here. That’s fine and we’ll say okay. So here’s a new method that I have and I want to go ahead and access my form variable. So I can just call it by its name form to control space and it will show me this form variable and that is the string. Okay. Equals five. That’s fine. Now what’s also cool about supplies I can also go up the chain so I can go from my form and I can take a look at these other variables that are in these global or solution level scopes. To do that I simply start typing the name of the scope. So you say scopes dot and then it’ll show you the two different scopes. I can say Bob Scope press a period and it will show me all of the variables that are in there. So there is that variable equals 10 and again scopes and global’s period and it will show me that global text. I can say equals 20. Likewise my global or solution level scopes can also create their own methods. So I’m going to create a new method in here. Bob Scope method. Here’s my method. So this global scope method can reference its own variables. Bob Scope variable. Right. It can also do the other global scope again using scopes. Global’s period. And I can go down and take a look at the variables or other data parts that are on forms. So to do that you start off with the keyword forms and it says people and then you can say period and there’s my form variable that’s in the form scope. Likewise I can access any other functions that are on there. So if I come over to my people and I make a new function do something save it. I can come back up here to my Bob Scope or my global scope. Let’s go up to Bob Scope and I can say forms people and now there’s the method do something. So I can execute a method that’s on a form just like from the form scope. I can also access global methods. Here’s my method my Bob Scope method. Well that’s about all you need to know about scopes. It’s a great concept that helps you keep your code organized. Well I hope you’ve enjoyed this video. Thanks for watching.