Well, there
is a way to create a new event handler function in run-time, but it's really nasty coding. I've been experimenting with this some time ago, but it's just plain ugly. Nevertheless, if it can help you or others, here it is:
Javascript code:
function CreateTheWindow( PlusWnd ) {
var sWndId;
// Code to retrieve the selected window ID goes here
// Assign a new event in the global scope
// Unfortunately, eval() is needed for this
eval( "On"+sWndId+"Event_Destroyed = OnCreatedWindowEvent_Destroyed" );
// Now the registered events have to be reloaded
// We'll be loading an empty JScript file to trigger the reloading
MsgPlus.LoadScriptFile("blank.js");
// Now that everything is set, create the window
var oWnd = MsgPlus.CreateWnd( "Interfaces.xml", sWndId );
}
// We'll make the new event handlers call this function
function OnCreatedWindowEvent_Destroyed( PlusWnd, ExitCode ) {
// Code goes here
}
I've tried many things already, but this is the best that I could get.
- In a JScript environment, global variables cannot be assigned by a variable name through normal code. In JavaScript on the contrary, there is the window object which controls all globally defined variables, allowing developers to use window['name'] to define a new global. The same thing goes for many other languages such as PHP, where you can use $$varname to create variables by a variable name. Unfortunately all of this can't be done in Plus! scripting without eval()...
- The created global function won't be registered as event handler right away either. In order for Plus! to re-register the global functions, another script file has to be loaded into the script using MsgPlus::LoadScriptFile.
So yes, in order to achieve this in a better way we'll need some changes in Plus! scripting itself. Here are my suggestions:
- Event handlers for all script windows (see CookieRevised's reply to [?] Detecting when a Plus! Window is closed).
- An OOP approach for event handlers:
Javascript code:
// DEMONSTRATION CODE
// This does not actually work at the time of writing!
var fEvent = function( PlusWnd, ExitCode ) {
// Code goes here
}
PlusWnd.AddEvent( 'Destroyed', fEvent );
This would not only allow new events to be created in run-time, but also add more than one handler to a single event. This would make it so much easier to manage the code: it would make it possible to put all code for one feature in separate script files instead of having to mix all code for the handlers in one event.
What do you think?