Tips:Using JavaScript in Memento: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
mNo edit summary
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Stamp|2017-02-17|4.2.1|1.0.8}}
{{Stamp|2017-02-20|4.2.1|1.0.8}}
 
'''[[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.
These are tips & techniques for using JavaScript for expressions in JavaScript fields and for scripts in triggers.


== FIELD tips ==
== Field tips ==
Tips for various field types.
Tips for calling ''field()'' for various field types.
{{FieldTypeReturnValues}}
{{FieldTypeReturnValues}}


Line 21: Line 19:
|-
|-
|<source lang="JavaScript">
|<source lang="JavaScript">
entry().set("percent", field("linkedLib.integerField")*100);
entry().set("percent", entry().field("linkedLib.integerField")*100);
</source>
</source>
|}
|}
==== Date & Time fields ====
JavaScript Date & time functions expect & return time '''in milliseconds'''. This differs from Calculation fields, which use seconds.
Also, Memento includes an optional JavaScript library '''moment.js''' that provides flexible formatting of dates and times. For a summary of this library, see [http://momentjs.com '''http://momentjs.com''']. For extensive details, see [http://momentjs.com/docs '''http://momentjs.com/docs''']. Skip over the first section "Where to Use It".<br/> Note that the particular library included in Memento at this time is called moment.min.js.
Also, see '''[[Date field]]''', '''[[DateTime field]]''', or '''[[Time field]]''' for general information about the Memento user interface for dates & times.


=== 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 ====
{|
{|
! JavaScript field
! JavaScript field
Line 34: Line 40:
</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.
This 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
Line 42: Line 48:
</source>
</source>
|}
|}
Alternatively, if the trigger script needs to handle each of the choices individually, it can be done add follows:
 
==== Handling checked items individually ====
If the trigger script needs to handle each of the choices individually, it can be done as follows:
{|
{|
! Trigger script
! Trigger script
Line 56: Line 64:
The Link to Entry field has its own structure, unique among the field types.
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 ====
==== Using ''field()'' to access the value of local and remote field values ====
: '''Argument'''
: '''Argument'''
:# The name of the field whose value you want
:# The name of the field whose value you want
Line 85: Line 93:
|}
|}
<br/>
<br/>
==== Using set() in a trigger to set the value of a local field ====
==== Using ''set()'' in a trigger to set the value of a local field ====
: '''Arguments'''
: '''Arguments'''
:# Name of the field whose value is to be set
:# Name of the field whose value is to be set
Line 102: Line 110:
<br/>
<br/>


=== To copy a link's value to another link ===
==== To copy a link's value to another link ====
:* The remote library must be set to use unique Entry Names.
:* The remote library must be set to use unique Entry Names.
:* Both links must point to the same library
:* Both links must point to the same library
Line 129: Line 137:
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:
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 ===
* 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 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.
* 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.
{|
{|
Line 147: Line 155:


=== Semicolons ===
=== 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.
In JavaScript, the use of semicolons is theoretically optional. In practice, however, without their use, problems arise in certain cases, and it is strongly recommended to use them 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, particularly in the case of JavaScript fields, in which the requirement is only to state about expression. 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 should obey strict JavaScript rules, including statement terminators.
 
== General JavaScript Tips ==
 
=== JavaScript Coding Style Guidelines ===
[[TBD]]


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.
=== JavaScript Best Practices ===
[[TBD]]


Trigger scripts are straight JavaScript, so the code must obey JavaScript rules, including statement terminators.
=== Common JavaScript Coding Errors ===
[[TBD]]


== Links ==
=== Links ===
[[JavaScript field]], [[Triggers]], [[Trigger Examples]]
[[JavaScript field]], [[Triggers]], [[Trigger Examples]]


Navigation menu