4 Replies Latest reply on Nov 28, 2007 11:25 AM by paulfeuer


      Hi All,
      New to Flex and OO in general, I used the CF extensions in FB2 to create a basic CRUD app that's grown into something more complex. Essentially, I pass some login credentials from a .cfm wrapper to my Flex app. In the top level application file (main.mxml) the "preinitialize" listener calls an init function where I set the incoming params (Flashvars) into a user class instance which is apparently accessible to the rest of the app and is used to set component element parameters based on a user's role. I would also like to create an ArrayCollection within that user class that is poulated by a database call in the same init function of main.mxml. I want to then populate comboBoxes in the subordinate components using the ArrayCollection in the user class object.
      While I seem to be able to access simple strings in the user object within the init functions (called by creationComplete listeners) of subordinate components, trying to access the ArrayCollection in the user object results in a null.
      In short, I don't get it. What is the best way to create application level variables/objects that can be accessed by subordinate components as they are created so I don't have to make the same database call in several components, just to fill a comboBox? Any input/guidance/training would be most appreciated. Perhaps an explanation on how the application objects are created (what order) would be helpful to more folks than just me.

      Happy Holidays to all....

          ntsiii Level 3
          There should be no problem accessing an arraycollection in a custom component as you are doing.

          Is the AC you are working with populated by a data service call? All dataq service calls are asynchronous. Do you have a timing issue?

            jgram Level 1
            Thanks Tracy. I thought it might be a timing issue since I am seeing the simple text in the user object, and yes I'm making a remoteObject call to a CFC to populate the arrayCollection. I'm guessing that the ac is not populated in time for the subordinate components to use it. Not sure how to control that or what better way to accomplish it.
            • 3. Re: NEED FOR GLOBAL VARIABLES?
              I think you are looking for a micro architecture called Cairngorm.
              This is a set of objects and design pattern / principles that defines a centralized container for your data models and a standardized way to implement the MVC design patterns for your FLEX applications. There is definately a slight learning curve but is is well worth it. I don't develop a FLEX app without it.


              Great article on how/what it does
              • 4. Re: NEED FOR GLOBAL VARIABLES?
                No need for anything other than basic actionscript.

                presumably, you're making a call using URLLoader or HTTPService. look at the documentation and see what event signals the completion of the call, and register a listener for it.

                to access the data that you've downloaded, the possibilities are virtually endless. one option is to use a singleton that performs the data fetch lazily. this means that your application has one static instance of your class, which would allow you to statically access it (e.g. MyDataManager.instance.theData )

                public class MyDataManager
                public static const instance:MyDataManager = new MyDataManager();
                private var _data:ArrayCollection;
                public function get theData():ArrayCollection { return _data; }

                Now the tricky part is managing the loading of the data and coordinating access to it so that if a client of the data wants it, but it's not finished loading, you can let the client register a callback function for when it is ready. or bypass that altogether and wait until the data is downloaded before initializing your other components.