Running the following piece of code in a script will register a javascript function named "afterSelectionChanged" to be run as soon as the selection is changed – the second parameter is the function name:
app.eventListeners.add("afterSelectionChanged", afterSelectionChanged);
My problem concerns unregistering this event listener, or rather preventing it from being registered again. I'm using a specified "targetengine", so running the script again and again will add a new function to be called every time the script is started, resulting in the same function being called multiple times.
In the main function (startup) of my script, I could loop through the app.eventListeners collection, and remove any eventListeners whose .handler == 'afterSelectionChanged()' or eventType == 'afterSelectionChanged' like this:
for (var elIndex = app.eventListeners.length-1; elIndex >=0; elIndex--){ if (app.eventListeners[elIndex].eventType == 'afterSelectionChanged'){ app.eventListeners[elIndex].remove(); } }
Is this a good practice?
Or is it unnecessary to check for specific event names at all, that is: should I rather remove all eventListeners, or could that disturb something? I have a #targetengine directive with a specified name, at the top of my script.
Are you supposed to try to remove all eventhandlers and / or kill your targeted engine when a ScriptUI window is closed?
Thanks,
Andreas