nexl object

Each JavaScript file is being provided with nexl API. The API is provided by nexl object.
nexl object provides following functions and objects to use within your JavaScript files.

nexl.args

Gives an access to arguments at a runtime. I.e. you can access HTTP arguments via nexl.args object:
myVar = 'Hello';
result = myVar + (nexl.args.myExternalArg || '');
myVar variable is concatenated with nexl.args.myExternalArg. If myExternalArg not provided as argument it will be replaced with empty string.

nexl.print()

nexl.print() function accepts one argument and prints it's value to the nexl log.

For example:
nexl.print('Person name is ' + person.name);
It will print a person.name value to the log.
Please pay attention nexl server log level must be at debug or silly level. Otherwise print will be ignored.

nexl.defaultExpression

To resolve a data from JavaScript file you have to provide nexl expression in HTTP request.
nexl allows to embed an expression into the JavaScript file instead passing it via HTTP request.

To embed nexl expression just assign it to the nexl.defaultExpression variable.

For example:
// ...
nexl.defaultExpression = '${arr1|arr2|concat()}'; // joining two arrays
//...
After that you don't need to provide the expression in HTTP request URL as expression=${arr1|arr2|concat()}.
The expression will be taken from the JavaScript file.

Please note you can always override a nexl.defaultExpression by expression provided in HTTP request.

Watch a demo

nexl.nexlize()

This function evaluates nexl expression.
It accepts two arguments: nexlExpression ( string ), arguments ( object ).
The second argument is optional.

This function can be used when your own JavaScript needs to evaluate nexl expression.

For example:
function makeComplexCalculations() {
    // resolving arr1, eliminating duplicate values, sorting
    var x = nexl.nexlize('${arr1#U#S}');

    // performing additional array aggregation...
    // ...

    return result;
}
The makeComplexCalculations() uses nexl API to evaluate the ${arr1#U#S} expression.

nexl.get()

This is a simplified version of nexl.nexlize() function when you just need to resolve a variable value.
This function also resolves arguments value ( when you use it inside JavaScript function ).

For example:
str1 = 'New';
str2 = 'York';
function demo() {
    return str1 + nexl.get('str2') + nexl.get('str3AsArg');
}
    
This function should be used when you don't know weather a variable provided as argument or it just a variable declared in your JavaScript file.

nexl.set()

This function sets ( or produces new ) JavaScript variables for current HTTP session ( JavaScript file remain unchanged ).
Generally you'll need it in your JavaScript functions ( in other cases you just assign a value to a variable ).

For example:
// ...
function produceNewVar() {
    nexl.set('weight', 68);
}
This will produce ( or override ) a weight = 68 JavaScript variable when you call a produceNewVar() function.

nexl.init

Function assigned to nexl.init variable will be automatically fired every time before nexl evaluates the expression passed via HTTP request ( a kind of interceptor ).
It can be useful when you need to perform some data aggregations before nexl expression is being executed but after the JavaScript is fully loaded when making an HTTP request.
It make sense when the JavaScript file is included by another JavaScript file and you need automatically run a JavaScript function after all files are fully loaded.

For example:
nexl.init = function() {
    // resolving an argument
    fullName = nexl.args.fullName;

    // extracting firstName and lastName with regex
    firstName = fullName.replace(/ .*/g, '');
    lastName = fullName.replace(/^[^ ]* /g, '');

    // producing firstName and lastName variables
    // JavaScript file remain unchanged, it valid only for current HTTP request
    nexl.set('firstName', firstName);
    nexl.set('lastName', lastName);

    // now you can reference firstName and lastName variables in nexl expression(s)
};

nexl.addInitFunc()

This function has similar functionality to nexl.init, but it allows to add multiple init functions ( as first parameter ) and specify their execution order ( as second parameter ).
Function with lower execution order fires first.
Zero priority equals to nexl.init function execution priority.

For example:
nexl.addInitFunc(function(){...}, 50);
nexl.addInitFunc(function(){...}, 10);
nexl.addInitFunc(function(){...}, -3);