Docs‎ > ‎Home‎ > ‎

JavaScript

This page provides some brief background for JavaScript.  It is oriented to programmers who are experienced in languages such as Java or C#, but new to JavaScript.  

It is intended to
  • provide basic background JavaScript information that you may need for using CA Live API Creator, and

  • explore various key elements of JavaScript that are relevant for using CA Live API Creator

JavaScript Usage

JavaScript is used in a number of different contexts, as briefly outlined below.

JavaScript in the Browser

This was the most common use of JavaScript, and its origin.  JavaScript is used to navigate the Document Object Model (fields in a Browser form), and handle events.

JavaScript to create Servers

Node.js is a popular approach for creating servers, providing a "single language" covering both the Browser and the Server.  This is very flexible, but very low level and rather complex.  For well-defined uses such as database access, specialized software (e.g., Live API Creator) can provide orders of magnitude greater speed and simplicity.

JavaScript as a scripting language

JavaScript makes an excellent scripting language.   Common uses are noted below.

Command Line Scripting

You can build new command language commands using JavaScript, in particular in Node.js.

Scripting inside a VM

It's increasingly common to use server-side JavaScript inside a JVM.  Attractively, the JavaScript code can access Java code inside the JVM, including data sharing.

This provides the simple interpreted / un-typed simplicity of JavaScript, in conjunction with Java.  Common implementations include Rhino and Nashorn.


JavaScript Language Concepts

It's been quipped that Java is to JavaScript as car is to carpet.  There's a lot of truth to that.  You can do a web search "JavaScript for Java programmers", and get a number of excellent summaries.

That said, you can write code that looks like this formula from the Live API Creator Demo API.  Note the intuitive if/else syntax, attribute reference, etc:

if (row.qty_ordered <= 6)  // discount (using conditional JavaScript logic)
   return row.product_price * row.qty_ordered;
else
   return row.product_price * row.qty_ordered * 0.8;


We won't repeat the Java vs. JavaScript descriptions here, but will focus on several that are relevant to Live API Creator, below.

Interpreted: agile

Unlike Java, you do not need to compile into class files.  This can simplify operations, since JavaScript code can be loaded and executed from text files, databases, etc.

Un-typed: simple (but code completion?)

Java is a strongly typed language.  This means many errors are caught at compile time.  This can make it robust for building large systems.

JavaScript is not typed, so you will see code like var theValue = "some string".  This can can be convenient, but you lose some services such as code completion in an Integrated Development Environment which depend on typing information.  We'd surely like to retain this capability.

Async functions: a significant challenge

Perhaps the biggest hurdle for many programmers arises out of browser-based usage, where synchronous operations (reading a file or a database) can block the browser.  This is serious and must be avoided.

So, JavaScript introduces the concept of asynchronous calls, with callbacks to process actual results.  Here is an example from Rowan Manning's blog:

console.log('First');
jQuery.get('page.html', function (data) {
    console.log("Second");    // <--- this happens last
});
console.log('Third');

As Rowan points, out, the result is "First", "Third", "Second".  That's because get is async, so execution proceeds past the statement (to "Third").  The "Second" executes lateronly when the get call completes.

If you are accustomed to believing code executes in the order written, well, this is different.  And even accepting it can lead to rather unruly programs (search on "callback hell").  We'd surely like to avoid this problem if possible.


JavaScript tooling


JavaScript tools for the Browser

jQuery is a common package that simplifies DOM access.  AngularJS is another common package that provides a Model View Controller framework for building applications.  There are many others.

Most Browsers provide tools to debug JavaScript.

NodeJS

NodeJS is a popular system - key elements useful to Live API Creator are described below.

NodeJS for Command Languages

Node packages can include information that make their functionality available in command lines.  This capability is used in the Node Admin CLI.

Node.js Installation

The node.js site contains installation instructions.

Node.js Package Installation: npm

Node.js has simplified installing packages with the conventions around npm.  This is a node-based command extension that will install the designated package.  For example, to install the Node Admin CLI.

npm install -g liveapicreator-cli

Node install operates in 2 ways:
  • By default, a node_modules directory is created where you run the command.  This makes the package available when running from that directory.

  • Or, you can specify the -g parameter, which installs it globally (so it's available in any directory)
Note: this of course requires you have write access to the folder used for the -g packages.  So, on Unix / Mac, the npm command often requires sudo when used with -g:

sudo npm install -g liveapicreator-cli



JavaScript and CA Live API Creator

Live API Creator employs JavaScript for event handling and functions, since:
  • JavaScript is a very popular language, used in virtually all systems.  Consequently, most programmers have some familiarity with it.

  • Its interpretive nature is suitable for no-compile / no-deploy API Creation

  • It provides the basic control flow and object access (row.balance) required

  • In the context of short event handlers, the un-typed nature of JavaScript is much less of an issue.

In particular, we employ JavaScript as a (server-side) scripting language in the API Server JVM (described above).  This means:
  • your JavaScript has access to other code running in the JVM

  • the API Server provides the threading functions, eliminating the troublesome "async issues" described above.  You can write simple loops to process results, and they run in the expected (synchronous) order

Finally, the API Creator JavaScript code editor provides code completion services for row variables.  This is possible since we know the context of a logic event (event for customer, for example), and can therefore provide the customer attributes in a list you can select from.

To sum up: API Creator uses JavaScript as a well-known and popular language to keep things agile, avoid the major pitfalls of JavaScript such as async programming, and retain the use of existing software running in the JVM.