Difference between revisions of "How:Write scripts in JavaScript for Memento"

From Memento Database Wiki
Jump to: navigation, search
Line 1: Line 1:

Revision as of 16:17, 9 May 2021

« Page as of 2021-05-09, editions Mobile 4.10.0, Desktop 1.10.0 »


The JavaScript field was the first JavaScript script type to be released. Prior to its introduction, the only way to calculate in Memento was with the Calculation field. With JavaScript there is a vast quantity of types of objects & methods (like:

  • arrays & sets
  • the ability to design & create custom object types with any methods (functions) you like
  • variable types (like very large integers, symbols)
  • and statements (like conditionals (if...else), custom function construction (function) or iteration (do...while), etc).

In short, a JavaScript has a plethora of features that make it tremendously more powerful than the Calculation field that came before it. By limiting yourself to a core subset of these features, you can keep the field type simple to learn & to use.

The JavaScript language

JavaScript was originally developed for programming Web sites. Since then, it has also been used as a language embedded within software tools like Memento, most prominently I've called Node.js.

The types of Memento JavaScript scripts are:

  • A JavaScript field within a library to calculate the values of expressions & formulas, often based on the values of other fields in the library or in other libraries that are linked
  • A trigger script, with event & phase options to perform custom calculations at certain crucial times, like before or after saving an entry or when a library or a field is opened
  • An action script, applying to the current entry or library to perform actions including actions on fields across all libraries in a user's database inundated by the touch or click of a button.
  • Shared scripts for use by trigger & action scripts to hold standard & often repeated code. The scripts are available to any other script, so you can change it in one place and have the effect in ask other scripts.
  • A custom data source script to guide source data from sources around the web or within the user's sphere of influence to the appropriate destination fields within the user's database.

JavaScript field

Like the Memento Calculation field type that game before it, the JavaScript field exists to produce ("return") a value calculated with an expression, which is a formula consisting of constants, variables, operators, and possibly statements.

Sample simple expression scripts

the value a hundred and twenty-three
the characters M e m e n t o as a string
2 * currentWeight
If currentWeight is 23 lbs, then 46 lbs
(hours * 60) + 10
If hours is 5 and minutes is 10, then 310

Sample complex expression script

Before release 4.10.0 on the mobile edition & 1.10.0 on the desktop edition, we had to validate entered data with a script like this one:

    // Validate entered value:

    var StdLimit = 100      // Our std for all products

    value = field("Value")  // Fetch value of field
    if (value <= StdLimit)  // If within limit,
      value                 //   Result is the value
    else                    // Otherwise,
      StdLimit              //   Result is the std limit


a value that has a string (text) value or a numeric (number) value.

String literal
"This is a string of textual characters"
String constant
Sol or PlanetMars
Number literal
123 or 314e-2 or -28.5
Numeric constant
Age or ListPrice
Symbol constant
a name representing a constant value, like pi or surname

a name representing a value that may change over time, like sum, currentValue, or orderDate
one or more characters that represent an expression, like + for plus or - for minus or * for multiplied by or / for divided by.
Some operators are made up of other operators used in combination, like <= for less than or equal to or && for and also.
in x++, ++ is a unary operator.
in b - 5, - is a binary operator.
in canDrive = age > 16 ? 'yes' : 'no', ? and : are used like if and else together as a ternary operator — the conditional operator. The > is a binary operator that returns true or false. If the result is true (the age is over 16), then canDrive will be true. If it is false, canDrive will be false. It is good for cases where something is this or else that — one or the other, plain & simple. Since it is plain & simple, it is (perhaps) appropriate to simplify the conditional as well, with ? and :.

Trigger script


Action script


Shared script


Custom data source script