I've created a Flex 4.6 web application that has many instances that need information from each other. Originally, I used a singleton to store the reference to the instance which was real convienent and works fine.
I am refactoring it to use FlexGlobals.topLevelApplication to access the instance instead of an extra singleton class.
Example 1:Singleton code that i am refactoring looks something like this and does work:(this works just fine but uses an extra class for storing the reference)
//ViewHDDComp.mxml
import HDD.Classes.HDDGlobals;
private var hdd:HDDGlobals= HDDGlobals.getInstance();
hdd.ViewHDD.menuButtons.visible=false; //the reference to the menuButtons component is stored in the HDD Globals
hdd.ViewHDD.menuInput.visible=false; //the reference to the menuInput component is stored in the HDD Globals
//HDDGlobals.as
private var _viewHDD:ViewHDDComp;
public function get ViewHDD():ViewHDDComp { return _viewHDD; }
public function set ViewHDD(viewHDD:ViewHDDComp):void{_viewHDD=viewHDD;}
Example 2: This is what I am trying to do which does not work:(don't understand why it doesn't)
//ViewHDDComp.mxml
import HDD.Components.ViewHDDComp;
private var h:ViewHDDComp=FlexGlobals.topLevelApplication.viewHDD;
h.menuButtons.visible=false; //menuButtons is a component in the instance viewHDD component
h.menuInput.visible=false; //menuInput is a component in the instance viewHDD component
This is what does work:(but is too verbose)
//ViewHDDComp.mxml
FlexGlobals.topLevelApplication.viewHDD.menuButtons.visible=false; //menuButtons is a component in the instance viewHDD component
FlexGlobals.topLevelApplication.viewHDD.menuInput.visible=false; //menuInput is a component in the instance viewHDD component
How do I abbreviate or type cast FlexGlobals.topLevelApplication.componentInstance.componentInstance.c omponentInstance?
Thanks in advance,
Joe
The Application object is a singleton and already has reference to its child elements. Seems like by creating the singleton, I am creating an extra class to simply hold some references to objects that already have a reference.
I am kinda confused as to why I can do
private var h:ViewHDDComp= HDDGlobals.getInstance().ViewHDD;
but not
private var h:ViewHDDComp=FlexGlobals.topLevelApplication.viewHDD;
I'm trying to condense my code a bit for readability and make memory improvements when possible.
hmm...Should I do a getInstance within the mxml component and reference it with:
private var h:ViewHDDComp=FlexGlobals.topLevelApplication.viewHDD.getInstance()
?
I have to say im not exactly sure
It’s interesting actually, Ive never really thought of doing it your way.
As it happens Im just about to go to bed, the perfect place to ponder on the question. J
I’ll have a look in the morning and try accessing some stuff the FlexGlobals way, and see how it works out.
I think Ive only ever referenced FlexGlobals.tLA when using it to position a pop up window.
Off the top of my head im not sure why your code isn’t working. It looks like it should
Anyway, I’ll have a look in the morning
Good luck and nice to speak to you
North America
Europe, Middle East and Africa
Asia Pacific