Docs‎ > ‎Home‎ > ‎

JavaScript

JavaScript is oriented to programmers who are experienced in languages such as Java or C#, but new to JavaScript. The information included on this page provides basic background JavaScript information that you may need for using API Creator and explores various key elements of JavaScript that are relevant for using API Creator.

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 Java virtual machine (JVM).

API Creator uses JavaScript in the following contexts:
  • JavaScript in the browser. 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 flexible, but low-level and rather complex. For well-defined uses such as database access, specialized software (for example, Live API Creator) can provide orders-of-magnitude greater speed and simplicity.
  • JavaScript as a scripting language. JavaScript for:
    • Command Line Scripting. You can build new command language commands using JavaScript, in particular in Node.js.
    • Scripting inside a VM. It is 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 has been quipped that Java is to JavaScript as car is to carpet. There is 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;

Live API Creator Descriptions

The following descriptions are relevant to Live API Creator:

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. JavaScript introduces the concept of asynchronous calls, with callbacks to process actual results.

The following code snippet 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 later, only 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. The following key NodeJS elements are useful to Live API Creator:
    • NodeJS for Command Languages. Node packages can include information that make their functionality available in command lines. The Node Admin CLI uses the Node packages.
    • Node.js Installation.
      For more information about installing Node.js, see the node.js site.
    • Node.js Package Installation: npm. Node.js has simplified installing packages with the conventions around npm. This is a node-based command extension that installs the designated package.
      For more information about installing the NodeJS command line utility (CLI), see the npm site.

JavaScript and Live API Creator

Live API Creator employs JavaScript for event handling and functions, since:
  • JavaScript is a 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 less of an issue.
Live API Creator employs JavaScript as a server-side scripting language in the API Server JVM. Your JavaScript has access to other code running in the JVM. 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. The API Creator JavaScript code editor provides code completion services for row variables. Live API Creator knows the context of a logic event (for example, the event for customer) and can therefore provide the customer attributes in a list you can select from.
Comments