Child pages
  • Implementing Business Logic

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note
titleNote

Developers who are familiar with Javascript may cite issues with browser support and speed of execution.

However, it is worth noting that Servoy does not deploy any Javascript. All code written in Servoy is deployed using Mozilla's Rhino project, which is an open-source, Java-based Javascript implementation.

This means that:

  1. All methods are executing in Java (orders of magnitude faster than interpreted Javascript)
  2. No business logic is ever exposed or executed in the browser, thereby eliminating browser support issues.
  3. Experienced developers can optionally use 3rd-party java APIs, mixing Java code directly in their Servoy methods.

Scope

AnchorscopescopeScope 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:

Code Block
titleGlobal Scope: Found in the globals.js file and accessible via the namespace globals, i.e.

...

code
globals.createNewCustomer(); // invokes the global method
Code Block
titleForm Scope: Found in

...

the formName.js file and accessible via the namespace forms.formName, i.e.
Code Block
forms.customers.controller.newRecord();  // invokes a form object from another scope

...

Code Block
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)

...

Way 1From context menu:

  1. From the Solution Explorer tree, navigate to the Active Solution > Globals > variables node.

  2. Right-click the variables node and select Create Variable from the pop-up menu.

  3. 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.

Way 2From Solution Explorer toolbar:

  1. From the Solution Explorer tree, navigate to the Active Solution > Globals > variables node.

  2. Highlight Select the variables node and click the Create Variable button from the lower toolbar in the Solution Explorer.

  3. 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.

Create a Form Variable (two ways) 

From context menu:

  1. From the Solution Explorer tree, navigate to the Active Solution > Forms > myForm > variables node.
  2. Right-click the variables node and select Create Variable from the pop-up menu.
  3. 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 Solution Explorer toolbar: 

  1. From the Solution Explorer tree, navigate to the Active Solution > Forms > myForm > variables node.

...


  1. Select the variables node and click the Create VariableOK button from the lower toolbar in the Solution Explorer.
  2. 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 context menu: 

  1. From the Solution Explorer tree, navigate to the Active Solution > Globals node.
  2. Right-click the Globals node and select Create Method from the pop-up menu.
  3. 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 Solution Explorer toolbar: 

  1. From the Solution Explorer tree, navigate to the Active Solution > Globals node.

...

  1. Select the Globals node and click the Create Method button from the lower toolbar in the Solution Explorer.
  2. 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 context menu: 

  1. From theSolution Explorertree, navigate to

...

  1. the Active Solution > Forms > myForm node.
  2. Right-click themyFormnode and selectCreate Methodfrom the pop-up menu.
  3. Choose a method name and clickOK. The method declaration will be generated in themyForm.jsfile, which will be opened in theScript Editor.

From Solution Explorer toolbar: 

  1. From the Solution Explorer tree, navigate to the Active Solution > Forms > myForm node.

...


  1. Select the myForm node and click the Create Method button from the lower toolbar in the Solution Explorer.
  2. 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

...

Any scripting API (including methods written by a developer) can be inserted directly into the Script Editor in two ways.

From context menu:  

  1. From the Solution Explorer tree, navigate to and

...

  1. select the node

...

  1. of a scriptable resource, (i.e. Active Solution > Forms > myForm > controller).
  2. 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 Solution Explorer toolbar: 

  1. From the Solution Explorer tree, navigate to and

...

  1. select the node

...

  1. of a scriptable resource.
  2.  In the list of methods and properties provided in the lower part of the Solution Explorer,

...

  1. select 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 context menu: 

  1. From the Solution Explorer tree, navigate to and

...

  1. select the node

...

  1. of a scriptable resource, (i.e. Active Solution > Forms > myForm > controller).
  2. 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 Solution Explorer toolbar: 

  1. From the Solution Explorer tree, navigate to and

...

  1. select the node

...

  1. of a scriptable resource.
  2. In the list of methods and properties provided in the lower part of the Solution Explorer,

...

  1. select 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.
Tip
titleTip

Any scripting API (including methods written by a developer) can be auto-completed using key strokes.

To use auto-complete, begin typing the code you wish to execute, then hit ctrl+space on the keyboard. A type-ahead list of available scripting objects will appear for you to choose from as you type. This is a highly productive and accurate way to write code when you have become more familiar with the APIs.

...