Skip to end of metadata
Go to start of metadata

You are viewing an old version of this content. View the current version.

Compare with Current View Version History

« Previous Version 19 Next »

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

  1. 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.
  2. 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
function main() {
  pjs.define("x", { type: "integer" });
  pjs.define("y", { type: "integer" });
  
  x = 1;
  y = 2;
  
  pjs.import("./main/increment.js", ["increment"]);
  
  increment();
  
  console.log(x, y);  // outputs 2  4
}
exports.run = main;
Importing all modules in a directory
function main() {
  pjs.define("x", { type: "integer" });
  pjs.define("y", { type: "integer" });
  
  x = 1;
  y = 2;
  
  pjs.import("./main/*.js");
  
  increment();
  decrement();
  decrement();
 
  console.log(x, y);  // outputs 0  0
}
exports.run = main;

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
function mainExternal() {
  let myOtherString = "I am imported.";
  return myOtherString;
}
exports.handleImport = mainExternal; //Assign a custom alias with the function name.

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
function mainLocal() {
  pjs.import("./myexternalfile.js"); //Import the custom module.
  let myString = "I am local.";
  pjs.messageBox(myString);
  myString = handleImport(); //Call the external function.
  pjs.messageBox(myString);
}
exports.run = mainLocal;

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.


  • No labels