Tips:Using JavaScript in Memento: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 29: | Line 29: | ||
=== Multiple-choice fields === | === Multiple-choice fields === | ||
The field() function returns an array of selected items from multiple-choice (Checkboxes & Multiple-choice list) fields. Therefore, if a comma-separated list of selections is desired for the result of a JavaScript field expression, it is necessary to fetch the items from the array and combine them into a string, separated by commas. This is easily done with the array method join(). | The ''field()'' function returns an array of selected items from multiple-choice (Checkboxes & Multiple-choice list) fields. Therefore, if a comma-separated list of selections is desired for the result of a JavaScript field expression, it is necessary to fetch the items from the array and combine them into a string, separated by commas. This is easily done with the array method ''join()''. | ||
==== Getting a comma-separated list of checked items from a multiple-choice field ==== | ==== Getting a comma-separated list of checked items from a multiple-choice field ==== | ||
{| | {| | ||
Line 38: | Line 38: | ||
</source> | </source> | ||
|} | |} | ||
The same solution is needed for trigger scripts that want to use such a comma-separated string, such as for the set() method of the Entry object. | The same solution is needed for trigger scripts that want to use such a comma-separated string, such as for the ''set()'' method of the Entry object. | ||
{| | {| | ||
! Trigger script | ! Trigger script |
Revision as of 00:50, 22 February 2017
These are tips & techniques for using JavaScript for expressions in JavaScript fields and for scripts in triggers.
Field tips
Tips for calling field() for various field types.
Field type | JavaScript object | Examples |
---|---|---|
Text , Hyperlink, Password, Phone, Email, Barcode, Radio buttons, Single-choice list, Calculation (string result) | String | "abc" |
Integer, Real number, Integer values, Rating, Currency, Calculation (number result) | Number | 42, 42.1 |
Date , DateTime, Time | Date | |
Checkbox (Boolean) | Boolean | true, false |
Checkboxes, Multiple-choice list | Array of strings | ["Shirts", "Pants", "Socks"] |
Image | Array of strings | ["file:///storage/emulated/0/DCIM/image01.png", "file:///storage/emulated/0/DCIM/image02.png"] |
Link to entry | Array of entries Each array element has a method field() for access to values of fields of entries of the linked library. Example: field("localField")[i].field("remoteField") |
[e1, e2, e3, ...] |
Simple fields
Fields that function as JavaScript type String, Number, Boolean, and Date operate in a direct manner. For example:
JavaScript field |
---|
field("linkedLib.integerField")*100
|
Trigger script |
entry().set("percent", field("linkedLib.integerField")*100);
|
Date & Time fields
JavaScript Date & time functions expect & return time in milliseconds. This differs from Calculation fields, which use seconds.
See Date field, DateTime field, or Time field for details of formatting date & time values.
Multiple-choice fields
The field() function returns an array of selected items from multiple-choice (Checkboxes & Multiple-choice list) fields. Therefore, if a comma-separated list of selections is desired for the result of a JavaScript field expression, it is necessary to fetch the items from the array and combine them into a string, separated by commas. This is easily done with the array method join().
Getting a comma-separated list of checked items from a multiple-choice field
JavaScript field |
---|
field("Menu Choices").join()
|
The same solution is needed for trigger scripts that want to use such a comma-separated string, such as for the set() method of the Entry object.
Trigger script |
---|
entry().set("menuSelections"), entry().field("remoteLibrary.menuChoices").join());
|
Alternatively, if the trigger script needs to handle each of the choices individually, it can be done as follows:
Handling checked items individually
Trigger script | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
var e = entry(), items = e.field("remoteLibrary.menuChoices");
for (var item in items) {
// Handle each item (items[item]) here
}
Link to Entry fieldsThe Link to Entry field has its own structure, unique among the field types. Using field() to access the value of local and remote field values
Using set() in a trigger to set the value of a local field
To copy a link's value to another link
How Trigger & JavaScript field scripts differYou'll notice in this page that the code for JavaScript fields and that for triggers are very similar, but also that they different. Here's how they are different: Context
SemicolonsTo make things as simple and straightforward as possible, not only is context set up for you in JavaScript fields, but the need for using JavaScript semicolons is removed. If you know where to put semicolons, their use is recommended, to guarantee clarity and correctness. However, if you aren't sure about such things, you can omit them, and most of the time there will be no consequences. Most JavaScript fields are just one or two lines long, so the need for statement terminators is minimal. Trigger scripts are straight JavaScript, so the code must obey JavaScript rules, including statement terminators. LinksJavaScript field, Triggers, Trigger Examples
|