Triggers: Difference between revisions

252 bytes added ,  23 August 2023
m
mNo edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Stamp|2021-03-31|4.10.0|1.9.6}}
{{Stamp|2023-08-12|5.0|2.0}}
{{BannerMenu}}
{{PageTitle|Triggers}}


A '''''Trigger''''' is a script that defines the processing of an entry whenever a particular kind of event takes place. Trigger scripts are written in '''[https://developer.mozilla.org/en-US/docs/web/JavaScript JavaScript]'''. When a trigger script is executed, it may perform actions like changing an existing entry, creating a new entry, executing an HTTP request, creating a file, performing data validation, etc.
A '''''Trigger''''' is a script that defines the processing of an entry whenever a particular kind of event takes place. Trigger scripts are written in '''[https://developer.mozilla.org/en-US/docs/web/JavaScript JavaScript]'''. When a trigger script is executed, it may perform actions like changing an existing entry, creating a new entry, executing an HTTP request, creating a file, performing data validation, etc.
Line 92: Line 94:
=== Opening a library ===
=== Opening a library ===
This event occurs when a library is opened before any activity is performed on the library.
This event occurs when a library is opened before any activity is performed on the library.
==== Phase: Opening the library ====
This event has only one phase, and it is ''synchronous''. Therefore, the script will fully execute before any other activity takes place in the library.
This event has only one phase, and it is synchronous. Therefore, the script will fully execute before any other activity takes place in the library.


=== Creating an entry ===
=== Creating an entry ===
Line 105: Line 106:
:; 3. After saving the entry (asynchronous): The script will be called once the save of the entry has been initiated and will continue in parallel with final processing of the new entry.<br/>If you want to do something whenever a new entry is added, but not before the data is saved, this is the place to do it.
:; 3. After saving the entry (asynchronous): The script will be called once the save of the entry has been initiated and will continue in parallel with final processing of the new entry.<br/>If you want to do something whenever a new entry is added, but not before the data is saved, this is the place to do it.


==== Scripting considerations: Creating an entry ====
==== Scripting considerations ====
''Creating'' scripts are different from other trigger scripts in that, since the new entry is being crafted, it is not yet stored within Memento, so it needs to be handled differently.


Use the method ''defaultEntry()'', rather than ''entry()'', to get the DefaultEntry object (the unstored template, if you will, for the eventual Entry object).
'''=>''''' Say what here? '''''
 
===== Creating an entry =====
''Creating'' scripts are different from other trigger scripts in that, since the new entry is being crafted, it is not yet stored within Memento &mdash; the Entry object does not yet exist &mdash; so it needs to be handled differently.
 
Use the method ''defaultEntry()'', rather than ''entry()'', to get the DefaultEntry object (the ''un''stored template, if you will, for the eventual Entry object).


Once the default entry is saved, it can be referred to by calling ''entry()'' to get the saved Entry object. So ''Before saving'' scripts use ''defaultEntry()'', and ''After saving'' scripts use ''entry()''.
Once the default entry is saved, it can be referred to by calling ''entry()'' to get the saved Entry object. So ''Before saving'' scripts use ''defaultEntry()'', and ''After saving'' scripts use ''entry()''.
Line 214: Line 219:


=== Writing a Trigger Script ===
=== Writing a Trigger Script ===
Trigger scripts are JavaScripts. See '''''[[#JavaScript links|links to JavaScript documentation]]''''' below.
Trigger scripts are in the JavaScript language. See '''''[[#JavaScript links|Links to JavaScript documentation]]''''' below.


; Event & Phase: Make sure to understand the Event & Phase you are scripting. This will dictate a number of things, such as whether data is present already or not, whether ''cancel()'' makes sense or not, whether the user is waiting for script execution or not, and so on.
; Event & Phase: Make sure to understand the Event & Phase you are scripting. This will dictate a number of things, such as whether data is present already or not, whether ''cancel()'' makes sense or not, whether the user is waiting for script execution or not, and so on.
Line 237: Line 242:
; [[Memento JavaScript Library]]: Memento JavaScript functions & objects<br/>
; [[Memento JavaScript Library]]: Memento JavaScript functions & objects<br/>
; [[Trigger Examples]]: Examples of trigger scripts for common needs
; [[Trigger Examples]]: Examples of trigger scripts for common needs
; [[How:Write scripts in JavaScript for Memento]]: Guidelines for writing JavaScript scripts for Memento


[[Category:Spec]]
[[Category:en]] [[Category:Spec]] [[Category:Sc]]