Assign Variables With a Custom Function

This action uses a custom function to assign values to a set of variables.

A custom assign variable function can perform a transformation on the source value before assignment.

The AssignVariablesAction will first look up the function referenced by "functionName" from the page's functions module and call it with the current available scopes. It will then assign the return value of the function call to the target variable. The custom function should have the following signature:

PageModule.prototype.myAssignVariableFunction = function (helper, targetDefaultValue)

The "targetDefaultValue" is the default value for the target which can be used to emulate the "toDefault" reset option.

The "helper" is an utility object that can be used to retrieve values for variables within the current scope and perform auto-assignment. It has the following interface:

class AssignmentHelper {
  /**
   * Gets a variable from a scope by its string representation, e.g.,
   * helper.get("$page.variables.myVar")
   */
  get(expr);
  
  /**
   * Assigns properties from one or more sources to the target if and
   * only if the property already exists on the target. The sources
   * are processed in the order they are defined.
   *
   * If target is null, any empty target value will be created based
   * on the target's type. If the target is not null, it will be cloned
   * and the sources will be assigned into the clone. In either case,
   * this value will be returned as the result.
   */
   pick(target, ...sources) {
}

Example: an assign variable function that resets the target value to its default value and auto-assign the source to the target:

PageModule.prototype.myAssignVariableFunction = function (helper, targetDefaultValue) {
  var source = helper.get("$page.variables.source");
  var result = helper.pick(targetDefaultValue, source);
  return result;
}