pjs.import()
This API is used to load exported functions from a Profound.js module into the current scope, while injecting all strongly defined fields and tables from the current scope into those functions.
Parameters
Module path / name - A String containing the module file name to load. An absolute path, a relative path starting with ./ or ../, or a path relative to the 'modules' directory can be used. If only the file name is given, the pathlist will be searched to locate the module. If the file extension is omitted, ".js" is assumed. When specifying the path, a file pattern (e.g. "./dirname/*.js") can be used to load multiple modules.
Import List (optional) - An Array of function names to import. If omitted, all exported functions are imported.
Exception Handling
An Error will be thrown if there are any problems loading the module, such as a file not found or syntax error.
Example 1.
Module 1 - increment.js
function increment() {
// x and y must be declared in the parent scope where pjs.import() is used
x += 1;
y += 2;
}
exports.increment = increment;
Module 2 - decrement.js
function decrement() {
// x and y must be declared in the parent scope where pjs.import() is used
x -= 1;
y -= 2;
}
exports.decrement = decrement;
Importing one module
function main() {
pjs.define("x", { type: "integer" });
pjs.define("y", { type: "integer" });
x = 1;
y = 2;
pjs.import("./main/increment.js");
increment();
console.log(x, y); // outputs 2 4
}
exports.run = main;
Importing one module and providing an import list
Importing all modules in a directory
Example 2.
2.1. Create a module containing the external function to called by the main app. [ Reference: Creating Callable Profound.js Modules. https://profoundlogicsupport.atlassian.net/wiki/x/LALOCQ ]
Module - myexternalfile.js
2.2. Create a module that serves as the main app, configured as the App Start File or Route. [ Reference: Using Workspaces, "Configuring Routes to Run Profound.js Modules". https://profoundlogicsupport.atlassian.net/wiki/x/BRDOCQ ]
App Start File/Route - mylocalapp.js
2.3. On the Profound.js IDE, go to [ Home Ribbon -> Launch -> Launch App in Browser Tab ]. The app will display "local" and "imported" successively on message boxes.
External Definitions (_definitions.js)
Strongly defined fields can be organized into a separate file named _defintions.js
, provided as an exported array of definitions. For example:
Each entry would specify a definition api
, such as “define”, “defineDisplay”, “defineTable”, “definePrinter”, “defineProc”, or “include”. If the api
property is not specified, “define” is assumed. The entry can also specify the following:
name
- the name of the field, file, or procedure being definedfile
- the file or table name being referred toconfig
- the definition configuration as specified by the corresponding API documentation
The “include” api
brings in other definition files, which can export another array of definitions or provide Profound.js code that defines fields, files, or procedures.
Benefits of external definitions
There are several advantages of using external definitions:
It keeps your code tidy and better organized.
It enables Profound.js to preprocess definitions, allowing flexibility in how the definitions are ordered. For example, one field definition can refer to another field’s definition in its configuration, even if that other field is defined later in the list.
It eliminates the need to specify
exports.parms
for describing module parameters to Proxy Programs and the PJSCALL command. Instead, the parameter information can be read directly from the external definitions file.