Tips:Using JavaScript in Memento: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 26: | Line 26: | ||
=== Multiple-choice fields === | === Multiple-choice fields === | ||
Multiple-choice (Checkboxes & Multiple-choice list) fields return an array of selected items. 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(). | |||
{| | |||
! JavaScript field | |||
|- | |||
|<source lang="JavaScript"> | |||
field("Menu Choices").join() | |||
</source> | |||
|} | |||
The same solution is needed for trigger scripts that want to use such a comma-separated string, such as the set() method of the Entry object. | |||
{| | |||
! Trigger Script | |||
|- | |||
|<source lang="JavaScript"> | |||
entry().set("menuSelections"), entry().field("remoteLibrary.menuChoices").join()); | |||
</source> | |||
|} | |||
=== Link to Entry fields === | === Link to Entry fields === |
Revision as of 11:42, 20 February 2017
This page is under development. Information is incomplete and potentially incorrect.
These are tips & techniques for using JavaScript for expressions in JavaScript fields and for scripts in triggers.
FIELD tips
Tips 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);
|
Multiple-choice fields
Multiple-choice (Checkboxes & Multiple-choice list) fields return an array of selected items. 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().
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 the set() method of the Entry object.
Trigger Script |
---|
entry().set("menuSelections"), entry().field("remoteLibrary.menuChoices").join());
|
Link to Entry fields
The 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
- Argument
- The name of the field whose value you want
- Returns
- An array of Entry objects.
- To access fields in the linked library, use:
JavaScript field var links = field("Link field name"); if (links.length > 0) { var name = field(links[0].field("Name"); // Put your code to handle name here }
Trigger var e = entry(), links = e.field("Link field name"); if (links.length > 0) { var name = e.field(links[0].field("Name")); // Put your code to handle name here }
Using set() in a trigger to set the value of a local field
- Arguments
- Name of the field whose value is to be set
- Comma-separated list of Entry Names
- To set a link field to point to an entry of another library and set another link field to point to two entries, use:
Trigger |
---|
var e = entry();
e.set("First link", "InterOpera Group");
e.set("Second link", "Mozilla,Google");
|
To copy a link's value to another link
- The remote library must be set to use unique Entry Names.
- Both links must point to the same library
Trigger |
---|
var e = entry(),
library = "Software firms",
from = "First link",
to = "Second link",
lib = libByName(library);
if (lib != null) {
// Copy the link
var entryNames = [],
entries = e.field(from);
for (var i in entries)
entryNames.push(entries[i].title);
e.set(to, entryNames.join());
}
|
How Trigger & JavaScript field scripts differ
You'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
- Context is set for you in a JavaScript field, and that context is the current entry. So, if you call field(), that implicitly invokes the field() function on the current entry.
- Context is not set in a trigger, so only global functions are initially available, and using those, you can set up context for functions you want to call on the objects you're interested in.
JavaScript field |
---|
field("Fraction") * 100
|
Trigger |
var e = entry(), lib = lib();
message("Library " + lib.title + ", entry " + e.title);
|
Semicolons
To 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.
Links
JavaScript field, Triggers, Trigger Examples
W3Schools | |
---|---|
JavaScript Tutorial | A pleasant, fairly complete, and useful tutorial on JavaScript Best on a computer or tablet in landscape. On a phone or tablet in portrait, scroll to the bottom for navigation. |
Mozilla Developer Network | |
JavaScript Guide | Shows you how to use JavaScript, gives an overview of the language, and presents its capabilities & features |
JavaScript Reference | The entire JavaScript language described in detail |
Introduction to JavaScript | Introduces JavaScript and discusses some of its fundamental concepts |
JavaScript Tutorial | A re-introduction. JavaScript is often derided as being a toy, but beneath its simplicity, powerful language features await. |
JavaScript 1.7 | The JavaScript release upon which Memento is currently based |
About JavaScript | Jumping off point in learning about JavaScript |