In my previous post I have illustrated the creation of a simple object manager to make a javascript application correctly dialog with a Flex movie. That is a simple and well functioning solution, however it is not suitable for a more complex context, when it is necessary, for instance, to use several Flex or Javascript/Ajax applications.
What is the right choice in such a situation? The solution often depends more on the developer's creativity than on the possibilities offered by the technology in use.
Let's think, for example, to a comparable situation in actual life: one or more people try to contact the same person that is not available at that moment. If the communication were not much important, each of them could simply try later on, with the hope to find the person available (previous solution), although in the meantime others might anticipate his/her call. What if the communication were extremely important, and risks to arrive too late (after our competitors' communication, for instance)? In order to avoid that possibility and all the related consequences, a voice mail (or a simple answering machine) could be used, as well as, at a later stage, a personal secretary could be hired. The messages will be recorded in their incoming order (for example using a queue or a single instance if we need only the last message), and as soon as the address is available again he/she will listen to the recorded messages or will be informed about them by his/her secretary. We shall now examine how to implement the easiest case, taking again as an example the disabilitation of the Flex application:
- We create a JS class named ObjectManager, it will represent our answering machine. We also create an empty constructor, in this case we shall not use status variables related to the Manager:
ObjectManager = function () {
}
If necessary we could insert variables such as this.available into the object in order to indicate its availability or to initialize global variables.
The only method which is necessary for the object manager's functioning is AddObject. With this function we'll have the possibility to add the desired object (e.g. a Flex movie) to the manager class:
ObjectManager.prototype.AddObject = function (objectName) {
if(eval('this.' + objectName +' == null')) {
eval('this.' + objectName + ' = new Object()');
eval('this.' + objectName + '.setStatus = null');
eval('this.' + objectName + '.enabled = 1'); //Default status value
return true;
}
else
return false; //object already registered
}
In this way, in the object manager will be created a simple object that we shall use to keep all the information which are necessary to guarantee the proper functioning of the application. Every object created in the object manager is directly linked to the object we want to control through the use of a unique string which represents it. For this reason it is necessary to use the function eval(string), as in the object manager we will not have direct reference to the objects to control.