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

From Memento Database Wiki
Jump to: navigation, search
m
m
Line 2: Line 2:
  
 
{{NoteLine|THIS PAGE IS UNDER DEVELOPMENT.}}
 
{{NoteLine|THIS PAGE IS UNDER DEVELOPMENT.}}
 +
 +
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 ==
 
== The JavaScript language ==
JavaScript is a language developed for programming Web sites. It has also been used as a language embedded within software tools like Memento, most prominently Node.js.
+
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:
 
The types of Memento JavaScript scripts are:
* A trigger script, with event & phase options
+
* 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
* An action script, applying to the current entry or library
+
* 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''
* Shared code for use by trigger & action scripts
+
 
* A custom data source script to guide source data to the appropriate destination fields
+
* 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 ==
 
== JavaScript field ==

Revision as of 15:30, 26 April 2021

« Page as of 2021-03-21, editions Mobile 4.10.0, Desktop 1.9.6 »


« NOTE: THIS PAGE IS UNDER DEVELOPMENT. »


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

123
the value a hundred and twenty-three
"Memento"
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

Terminology

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

String
"This is a string of textual characters"
Number
123 or 3.14 or -28.5
Symbol
a name representing a constant value, like pi, x, tareWeight, or surname

Variable
a name representing a value that may change over time, like sum, currentValue, or orderDate
Operator
one or more characters that represent the value of other values that go together in some fashion to determine a result, 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.


Trigger script

TBD

Action script

TBD

Shared script

TBD

Custom data source script

TBD