|
||
JavaScript in Composer
Composer uses JavaScript as its scripting language. The only differences are some Composer-related libraries which contain Composer classes to, for example, reference fields in a Composer form or for other Composer integrations.
This section of the Guide focuses on form input scripting. Elsewhere we will cover the XML code used to tweak the format the form's output for some special purpose (such as to accommodate some integration point with the Enterprise's backend).
The syntax for Composer's scripts is that of JavaScript. JavaScript is maintained by the Mozilla Foundation. Here are the canonical references:
•https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
•https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference
Other authoritative references are:
•http://www.w3schools.com/js/js_syntax.asp
•http://www.w3schools.com/jsref/default.asp
•http://www.tutorialspoint.com/javascript/javascript_syntax.htm
•http://www.java.com/en/download/faq/java_javascript.xml
In JavaScript, the "this" object generally refers to the current object receiving the event. Never use the "this" object in Composer scripts.
All Composer JavaScript (for rules, click action scripts, etc.) are embedded in a function, rather than being coded directly under an event. (The reasons for this are beyond this document's scope.) The commonly used "this" object, which refers to the current object, is not available in functions. Instead, Composer provides a "me" object. The me object refers to the object where the business rule is located.
If you have some existing JavaScript code that works correctly in either PDF or HTML, you can generally simply replace every occurrence of "this" with "me" for the same code to work in Composer.
If you want to reuse a legacy script in Composer and want it to work correctly across platforms, you must modify it to use the technology-independent Composer Framework JavaScript library instead of the native libraries. For example, use sfc.getRawValue(me) rather than me.rawValue. See the Avoka Composer Framework link on Composer's Welcome page.
Here, in the context of Transact Composer, "across platforms" specificity means that the sfc calls work for both PDF and HTML.
Many of the Composer functions, which are defined in sfcFunctions.js library, have been designed for your convenience — to accomplish a lot with little code. Many of these are embodied in the function buttons in the ScriptEditor.
The function buttons insert the corresponding function code into the Script tab's text window. That code ("$SUM(...)" for example) gets expanded further at runtime (to "sfcFunction.sum(...)" for example
We will also use the terminology "sfc classes" interchangeably with "Composer functions".
We will mention here a few of these sfc classes created for you convenience:
•sfc.copyValues (srcBlockNodeRef, targetBlockNodeRef);
though note that this does not work recursively, i.e. to blocks within blocks.
Composer also uses conveniently has a number of scripting conventions, such as parameters, which reduce the amounts of code needed to refer to form elements and their values. For more, see the Composer Framework.
There are a number of JavaScript tools on the web for checking syntax:
•http://www.javascriptlint.com/online_lint.php
A simple webpage, where you paste your JavaScript fragment into a textarea field and you get back a report of any simple syntax errors. Note that this tool is somewhat limited in scope, but this makes it very useful for quick checks.
Most modern browsers have JavaScript tools
•https://developers.google.com/chrome-developer-tools/docs/javascript-debugging
documentation for Chrome's inbuilt checker
•https://developer.mozilla.org/en-US/docs/Tools/Debugger
https://developer.mozilla.org/en/docs/Debugging_JavaScript
These document Firefox's inbuilt Web Console tool
•https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Debugger/Debugger.html
The documentation for Apple's Safari on OS X