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.

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

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):
Create a Form Variable (two ways):
Creating a Method
Create a Global Method in one of two ways:
Create a From Method in one of two ways:
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:

Insert Sample Code

Any scripting API's commented sample code can be inserted directly into the Script Editor in two ways:

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.

If you are unfamiliar with Javascript syntax, that's OK. In the Solution Explorer, navigate to the JS Lib node. This object contains a list of APIs for dealing with the Javascript language and native data types. It even contains syntax completion for common statements in the Statements node.

Quick Start Video