2022.2

Table Of Contents
Thefollowingsnippetdoesthesamefortherestoreprocess.
window.addEventListener('olcotgrestorestate', function(event) {
/* code to retrieve custom data and restore custom widgets */
});
ThefollowingcodecombinesthepreviouslysavedstringwiththerestoredURLofa(static)COTGCam-
eraelement:
window.addEventListener("olcotgrestorestate", function(event) {
var value = event.detail.state["myString"];
value = value + $("#camera1 img").attr("src");
$("form p").html(value);
};
Note: Youshouldnotregisterfortheseeventsafterthedocumenthasloaded(e.g.onthe$(doc-
ument).ready()event),becausetheseeventsmightgettriggeredbeforethedocumentis
ready.
PlaceyourcodeinaseparateJavaScriptfileandmakesuretoincludethatfileintheWebcontextorin
theWebsectionthatcontainstheCOTGForm(see"IncludingaJavaScriptfileinaWebcontext"on
page515).Forthisfile,theorderinwhichJavaScriptfilesareincludedinthetemplatedoesn'tmatter.
Handlingthesaveandrestoreevents
Thecodeinsidethefunctioncallbackoftheaddedeventlistenersshouldrespondtotheevent,bysav-
ingorretrievingcustominformation,respectively.
WhentheCOTGappsavestheForm,youcanstoreextrainformationintheevent.detail.state
objectasfollows:
event.detail.state["key"] = value;
Thekeycanbeanystring,aslongasthatstringisn'tthesameastheIDofoneofthewidgetsonthe
Form.
Inthecodethathandlestheolcotgrestorestateevent,usethesamekeytoretrievethestored
information.
Thefollowingsamplesavesthevalue"test"using"myString"asthekey,whentheFormissaved:
window.addEventListener("olcotgsavestate", function(event) {
event.detail.state["myString"] = "test";
});
ThefollowingcoderetrievesthevaluethatwasstoredwiththemyStringkeyanddisplaysitinapara-
graph(a<p>element)atthetopoftheform.
Page 552