JavaScript Control Using Library
  • 15 Jul 2021
  • 1 Minute to read
  • Dark
    Light
  This documentation version is deprecated, please click here for the latest version.

JavaScript Control Using Library

  • Dark
    Light

Article summary

Overview

This document discusses how to add a JavaScript Control using a third-party library.

Add Script Control

In order to add a JavaScript library for use in Decisions, a Script Control must be added.

  1. Select a Designer Folder, then click CREATE FORM from the Folder Actions Panel.
  2. Choose the JavaScript Control section from the left menu options.
  3. Select Add Script then click CREATE.java1.png
  4. In the Add JavaScript Control dialog, enter a name for the library.
  5. Upload the library .js file
    In the image below, the script control name is “preact” even though the file uploaded is “preact.min.js.”
    The name provided for the script will be the name that the other JavaScript controls will use at runtime.

    java2.png


Inside Your Control

Inside the JavaScript Control, hook into some exposed Decisions JavaScript to ensure the library is loaded.

PreactDemo.prototype.initialize = function(host, component) {
   // here “virtualPath” is a global variable in the Decisions client
   $.when(
     $DP.ScriptLoader.LoadScript(
       virtualPath + "/scripts/FormControls/jsFiles/preact.js"
     )
   ).then(function() {
      // Do initialization that depends on the 3rd party library
   }.bind(this));
 }  

For safety, it’s best to do the same check inside the other Decisions JavaScript Control API methods. e.g.

PreactDemo.prototype.setValue = function(data) {
   $.when(
     $DP.ScriptLoader.LoadScript(
       virtualPath + "/scripts/FormControls/jsFiles/preact.js"
     )
   ).then(function() { /* Update Control */ }.bind(this));
 }

Was this article helpful?