Table of Contents
Overview
While the Servoy platform is based entirely in Java, one does not need to write any Java during the course of development or deployment.
Instead, all business logic is implemented using Javascript. Javascript was selected because it is an internet standard, easy to learn and as such, the most widely used scripting language on the planet. Javascript is far more productive than coding in pure Java and Servoy provides robust APIs with which to quickly and easily implement business logic.
Scope
Scope defines the domain in which code is executed and subsequently determines the namespace by which scripted objects are referenced. Javascript code (functions and variables) may be defined in the following two scopes:
Global Scope: Found in the globals.js file and accessible via the namespace globals, i.e.
globals.createNewCustomer(); // invokes the global method
Form Scope: Found in the formName.js file and accessible via the namespace forms.formName, i.e.
forms.customers.controller.newRecord(); // invokes a form object from another scope
compared to:
function createNewCustomer(){ // a method defined within the 'customers' form scope
controller.newRecord(); // invokes the same form object from within the form's scope. Notice the fully qualified namespace is NOT required
}
Creating a Variable
Create a Global Variable (two ways):
- From the Solution Explorer tree, navigate to the Active Solution > Globals > variables node. Right-click the variables node and select Create Variable from the pop-up menu. Choose a variable name, a data type and optionally choose an initial value and click OK. The variable declaration will be generated in the globals.js file, which will be opened in the Script Editor.
- From the Solution Explorer tree, navigate to the Active Solution > Globals > variables node. Highlight the variables node and click the Create Variable button from the lower toolbar in the Solution Explorer. Choose a variable name, a data type and optionally choose an initial value and click OK. The variable declaration will be generated in the globals.js file, which will be opened in the Script Editor.
- From the Solution Explorer tree, navigate to the Active Solution > Forms > myForm > variables node. Right-click the variables node and select Create Variable from the pop-up menu. Choose a variable name, a data type and optionally choose an initial value and click OK. The variable declaration will be generated in the myForm.js file, which will be opened in the Script Editor.
- From the Solution Explorer tree, navigate to the Active Solution > Forms > myForm > variables node. Highlight the variables node and click the Create VariableOK button from the lower toolbar in the Solution Explorer. Choose a variable name, a data type and optionally choose an initial value and click. The variable declaration will be generated in the myForm.js file, which will be opened in the Script Editor.
Creating a Method
Create a Global Method in one of two ways:
- From the Solution Explorer tree, navigate to the Active Solution > Globals node. Right-click the Globals node and select Create Method from the pop-up menu. Choose a method name and click OK. The method declaration will be generated in the globals.js file, which will be opened in the Script Editor.
- From the Solution Explorer tree, navigate to the Active Solution > Globals node. Highlight the Globals node and click the Create Method button from the lower toolbar in the Solution Explorer. Choose a method name and click OK. The method declaration will be generated in the globals.js file, which will be opened in the Script Editor.
Create a From Method in one of two ways:
- From the Solution Explorer tree, navigate to the Active Solution > Forms > myForm node. Right-click the myForm node and select Create Method from the pop-up menu. Choose a method name and click OK. The method declaration will be generated in the myForm.js file, which will be opened in the Script Editor.
- From the Solution Explorer tree, navigate to the Active Solution > Forms > myForm node. Highlight the myForm node and click the Create Method button from the lower toolbar in the Solution Explorer. Choose a method name and click OK. The method declaration will be generated in the myForm.js file, which will be opened in the Script Editor.
Implementing Basic Business Logic
To implement some business logic, create a method and fill in the body of the Javascript function with executable code.
The following example implements the functionality to advance the selected record index on a form:
function nextRecord(){
var index = controller.getSelectedIndex(); // store the current index
controller.setSelectedIndex(index+1); // increment the index by 1
}
The example uses the form's controller object, part of the Javascript API provided by Servoy.
Developers need not memorize the API or look it up. The scripting APIs are self documenting, and code fragments can easily be inserted into the Script Editor.
Insert a Code Fragment
Any scripting API (including methods written by a developer) can be inserted directly into the Script Editor in two ways:
- From the Solution Explorer tree, navigate to and highlight a node for a scriptable resource, (i.e. Active Solution > Forms > myForm > controller). In the list of methods and properties provided in the lower part of the Solution Explorer, right-click the method or property that you wish to invoke and select Move Code. The code will be copied into the Script Editor and will be referenced with the correct namespace for the current scope. You may need to fill in specific arguments.
- From the Solution Explorer tree, navigate to and highlight a node for a scriptable resource. In the list of methods and properties provided in the lower part of the Solution Explorer, highlight the method or property that you wish to invoke and click the Move Code button in the lower toolbar of the Solution Explorer. The code will be copied into the Script Editor and will be referenced with the correct namespace for the current scope. You may need to fill in specific arguments.
Insert Sample Code
Any scripting API's commented sample code can be inserted directly into the Script Editor in two ways:
- From the Solution Explorer tree, navigate to and highlight a node for a scriptable resource, (i.e. Active Solution > Forms > myForm > controller). In the list of methods and properties provided in the lower part of the Solution Explorer, right-click the method or property that you wish to invoke and select Move Sample. A verbose, commented sample will be copied into the Script Editor.
- From the Solution Explorer tree, navigate to and highlight a node for a scriptable resource. In the list of methods and properties provided in the lower part of the Solution Explorer, highlight the method or property that you wish to invoke and click the Move Sample button in the lower toolbar of the Solution Explorer. A verbose, commented sample will be copied into the Script Editor.