Global Scripts

Global scripts are simple JavaScript files containing functions that can be used by other scripts, such as validation scripts and token procedures. Their main purpose is to make it possible for developers to re-use functionality between scripts, avoiding code duplication.

Global scripts allow the use of standard JavaScript libraries. Simply include the source code of your favourite JS library in a global script to start using it from any of your JS scripts.

An example global script may look like this (example taken from the Slang library)

Listing 214 Global script example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  // Capitalizes the first character of a string
  function capitalize(input) {
      return input.charAt(0).toUpperCase() + input.slice(1);
  }

  // Uncapitalizes the first character of a string
  function uncapitalize(input) {
      return input.charAt(0).toLowerCase() + input.slice(1);
  }

  // Capitalizes each word in the string
  function capitalizeWords(input) {
      return input.replace(/\w+/g, function(word) {
          return capitalize(word);
      });
  }

Global scripts are made available to all other types of procedures and do not need to be imported explicitly to use. In a sense they can be seen as added to the global namespace which makes it important carefully name functions in order not to pollute the namespace unnecessary.

Important

Global scripts are not executed when added to the configuration, so creating initilization code in this context is not possible. There is also no implicit order in which these are parsed and added, but rather all global scripts in the system can be assumed to be available to the other procedures when they are called.

Despite the name of the procedures can global scripts not be used to keep global state. They are made globally available but are not guaranteed to be the same instance between requests.

Common API

All global scripts are compiled in the same context as Curity procedures, and hence have access to all of the Common Procedure API.

Global Constants

It is possible to define constant variables in these scripts, but these declarations can only be accessed from within functions, since there is no guaranteed ordering of compilation.

Example of global map

Useful structure could be to maintain a semi-static map of settings that other procedures use. A global procedure could be defined as follows

Listing 215 Declaration of global map
1
var someMap = {"foo" : "bar", "zort" : 123};

Again, this will be possible to access within other procedures, but the following structure is NOT supported

Listing 216 Incorrect usage of global variables
1
2
3
var someMap = {"foo" : "bar", "zort" : 123};

var otherMap = someMap;   <--- ERROR

Instead the correct usage would be:

Listing 217 Correct usage of global variables
1
2
3
4
5
6
var someMap = {"foo" : "bar", "zort" : 123};

function foobar() {
  var foo = someMap["foo"]   <--- CORRECT
  //Do something with foo
};