|
-
code change over to Switch / Case ....
Hi there,
I have this code which I have used for a long time in my AIR Apps which have a few modules only, but I wanted to test it and maybe use it also now in my other Flex Apps, but here I have many modules and I can't find the right approach to converting this code into something like a Switch / case to be able to cut down on code lines and have this working effectively there also !
Any help would be appreciated I have tried a few things yet always seem to get stuck.
The code I have used until now and which worked very well in AIR Apps:
Code:
<mx:Script>
<![CDATA[
protected function modIntro_activateHandler(event:Event):void {
var loaded:Boolean;
var childDomain:ApplicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
if (loaded) unload(); else load();
function load():void {
loaded = true;
modIntroStep.applicationDomain = childDomain;
modIntroStep.loadModule("StepIntroGeneralENE.swf");
loaded = false;
childDomain = null;
}
function unload():void {
loaded = false;
modIntroStep.applicationDomain = null;
modIntroStep.unloadModule();
/* loaded = false; */
childDomain = null;
modIntroStep.deleteReferenceOnParentDocument(this);
startGCCycle();
}
}
]]>
</mx:Script>
<mx:ModuleLoader id="modIntroStep"/>
The code for the next module would be more or less the same:
Code:
<mx:Script>
<![CDATA[
protected function modStep1_activateHandler(event:Event):void {
var loaded:Boolean;
var childDomain:ApplicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
if (loaded) unload(); else load();
function load():void {
loaded = true;
modStep1.applicationDomain = childDomain;
modStep1.loadModule("StepIntroGeneralDEE.swf");
loaded = false;
childDomain = null;
}
function unload():void {
loaded = false;
modStep1.applicationDomain = null;
modStep1.unloadModule();
/* loaded = false; */
childDomain = null;
modStep1.deleteReferenceOnParentDocument(this);
startGCCycle();
}
}
]]>
</mx:Script>
<mx:ModuleLoader id="modStep1"/>
-
Senior Member
Hi there
I can't really tell the difference between the two bits of code - is it just that in the first you're manipulating modIntroStep and in the second modStep1? If that's the only difference, then you just want some kind of way to determine which module needs to be loaded / unloaded. What is dispatching the Event? Maybe than can help. Maybe you could write your own extended Event that contains information about which module is to be used.
Though I may have missed the point, I'm not sure...
-
Hi there,
YES, the code difference lies only in the modules Name which is to be loaded.
The 'modIntroStep' and the 'modStep1' are just the different modules named as an example which will become in my new App. Intro To 96 ....
And YES the code is always the same right through yet my difficulty is to set it properly into a Switch / Case statement with this 'If Loaded - UnLoad/Load' statement.
I have tried many ways, but could not come up with the right answer. My intention was to use this code with many modules yet save drastically on code lines.
As you said I want to find a way To find a 'kind of way to determine which module needs to be loaded / unloaded' regards aktell
-
Senior Member
You create one function:
protected function modIntro_activateHandler(event:Event):void
{
var myTarget:String;
switch (event.currentTarget.toString())
{
case "A"
myTarget="StepIntroGeneralENE.swf";
break;
case "B"
myTarget="StepIntroGeneralDEE.swf";
break;
}
if (loaded) unload(); else load();
function load():void {
loaded = true;
modIntroStep.applicationDomain = childDomain;
modIntroStep.loadModule(myTarget);
loaded = false;
childDomain = null;
}
function unload():void
{
loaded = false;
modIntroStep.applicationDomain = null;
modIntroStep.unloadModule();
/* loaded = false; */
childDomain = null;
modIntroStep.deleteReferenceOnParentDocument(this) ;
startGCCycle();
}
}
- The right of the People to create Flash movies shall not be infringed. -
-
Senior Member
so cancerinform is assuming that the name of the event dispatcher (event.target.name) indicates which module to load. Is that the case? The thing is we need to know what information is available to the event handler.
I would avoid the switch statement altogether if possible. If we go with cancerinforms idea that it's based on the event target's name, then I would create an object somewhere that behaves as an associative array, and have something like
PHP Code:
//this is pseudo-code because I don't know where the modules get their variable names
var modules:Object = {}
FOR EACH MODULE:
modules[MODULENAME] = MODULE
//then in the event handler
protected function activateHandler(event:Event):void
{
var myTarget:MODULETYPE = modules[event.target.name]
var loaded:Boolean;
var childDomain:ApplicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
if (loaded) unload(); else load();
function load():void
{
loaded = true;
myTarget.applicationDomain = childDomain;
myTarget.loadModule(MODULE_PATH); //Again, I don't know enough about the project to indicate how you would access the path
loaded = false;
childDomain = null;
}
function unload():void
{
loaded = false;
myTarget.applicationDomain = null;
myTarget.unloadModule();
/* loaded = false; */
childDomain = null;
myTarget.deleteReferenceOnParentDocument(this);
startGCCycle();
}
}
-
Senior Member
It appears from the example that the id (not name) of the event target is always different, since the function names are different. However, this is something aktell needs to tell us.
- The right of the People to create Flash movies shall not be infringed. -
-
Senior Member
Ooops, misread your code. But yeah, that's the problem - could be it's the same EventDispatcher that dispatches each time, in which case event.target will always be the same, and therefore wouldn't allow you to access the relevant module.
-
Hi there,
Thanks a lot for all your input which is very much appreciated !!!
My code which I send worked via a (Tree / ViewStack) by selecting a Tree Node I choose my module within the ViewStack with handleChange(event) , and by this selection I used the modIntro_activateHandler(event) with the code etc, but this only worked in AIR yet in Flex I do need to use it as modIntro_ creationComplete Handler(event).
All this is not of much Importance as it is code which needs to be transformed to suite as you both very well showed and pointed out; I guess using it with the ViewStack handleChange(event) would be fine and maybe calling it in a separate function.
The main issue I think for me is that it would have to be addressed at each module change in the ViewStack reading out the right module Load/Unload with as few code lines as possible.
regards aktell
-
I have worked for a few hours with these items you send, and the modules did not want to show at all with the first code block, and I have tried in particular to understand the second code block the one without the Switch/Case statement.
What is there in particular you need to know about this I admit do not understand it very well and I think my answers are quiet wrong so please if you could be a little bit more specific e.g.
// This is pseudo-code because I don't know where the modules get their variable names - and -
// Again, I don't know enough about the project to indicate how you would access the path.
Project Details:
The project is simple a Flex web Application which works as a large big Menu for around 92 modules at the moment and maybe for several hundred more in time to come. A menu; where as you select the first set of main modules through a Tree Node and a second set of sub modules within the same Tree.
Within these sub modules there are again sub modules (A to Z) Now each of these modules are a country say A = Australia, Austria etc. and each of these when showing and are active 6 small Images etc. with Links to a particular Application which opens within a new window and totally independent from the Main MENU App.
And that is it very strait forward and simple and working as is very well yet I wanted to improve the code and streamline it for more performance which works very well here as well with the code supplied, but again far to much code lines if used this way. regards aktell
Last edited by aktell; 07-10-2012 at 01:57 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|