7 Replies Latest reply on Apr 24, 2009 2:17 AM by TodLoch

    Is it a good practise to do it in this way.. (access to global variable)

    TodLoch Level 1

      Hi,
      I would like to ask, if it is a good practise to do it in this way:....

      I have a main.mxml with code like this:

      <!-- main.mxml-->
      ......

      <mx:Script source="includes/main.as"/>

      <!-- ListaPolis is my custom components in namespace oil-->

      <oil:ListaPolis left="10" top="0" id="listaPolis">
      </oil:ListaPolis>

      ........

       


      In main.as file I have declared:
      ...

      public var ac_php_results:ArrayCollection = new ArrayCollection();

      ...


      Now, I would like to use ac_php_results variable in my custom components oil::ListaPolis, so I do it in this way:

      <!--ListaPolis.mxml-->
      ....
      mx.core.Application.application.ac_php_results.addItem(.......);

      ....

       

      This works, but I wonder if I could do this in another way, not using mx.core.Application.application.
      Without this I receive error, about unknown variable.


      Best regards,
      Mariusz

        • 1. Re: Is it a good practise to do it in this way.. (access to global variable)
          maniChina

          I think you confused about the domain of the variable, "ac_php_results" just could be directly used in Application domain.

          I think you should create other singleton class to used between Application and your custom Component.

          • 2. Re: Is it a good practise to do it in this way.. (access to global variable)
            wvxvw Level 1

            See this example on how you establish connection between two MXML components w/o using Application.application

            http://www.actionscript.org/forums/showthread.php3?t=193795

            Besides, avoid underscores in the variable names, this is because you don't know how generated methods / getters / setters are called, and if you do put underscores in the variable names you have a good chance to have a name collision with the generated ones.

            Yet another thing - MXML file names should start with the capital letter, this is because they are classes.

             

            More than that, if you do need a global access to the variable, this should light you a red bulb, as in general global variables, singletones and such are considered bad practice. This is becuase it makes your code into non reusable, which makes you loose one of the advantages of OOP approach - reusability / incapsulation.

            • 3. Re: Is it a good practise to do it in this way.. (access to global variable)

              Sorta defeats the purpose of the custom component i.e. an independant module. You could define a public variable in your custom component. Import the component into the main app (as you are now doing) and then set this public variable from the main app i.e.

               

              In your custom component:

                   public var ac_php_results_from_app:ArrayCollection;

               

              In the main app:

                   import ListaPolis;

               

                   then once ac_php_results are received:

                   ListaPolis.ac_php_results_from_app = ac_php_results.

              • 4. Re: Is it a good practise to do it in this way.. (access to global variable)
                ergo_eleven Level 1

                You thought is in the right way!

                It's bad practice referencing to Application pointer.
                Generaly says you should split you application into Model-Control-View. In a Model you will store all data that came from server. View should take its data from Model. And in a Control you do populating Model's data.

                • 5. Re: Is it a good practise to do it in this way.. (access to global variable)
                  TodLoch Level 1

                  I'm afraid this example is too complicated for me, because I just start with Flex :-((

                   

                  Could you give me simpler example?

                   

                  I check that even I want to use variable in my application's main file:

                   

                  THIS WAY WORKS:

                   

                  <!-- MainApps.mxml-->
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

                  ......

                  <mx:Script source="includes/main.as"/>

                   

                  <mx:Script>
                  <![CDATA[
                                         mx.core.Application.application.ac_php_results.addItem(...some object....);
                  ]]>
                  </mx:Script> 
                  ........

                  </mx:Application>

                   

                   

                  BUT THIS DOESN'T:

                   

                  <!-- MainApps.mxml-->
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

                  ......

                  <mx:Script source="includes/main.as"/>

                   

                  <mx:Script>
                  <![CDATA[
                                         ac_php_results.addItem(...some object....);
                  ]]>
                  </mx:Script> 
                  ........

                  </mx:Application>

                   

                   

                  In main.as file I have declared:
                  ...

                  public var ac_php_results:ArrayCollection = new ArrayCollection();

                  ...


                  main.as file is a file where I declare a few variables which I would like to use in many different custom components

                  • 6. Re: Is it a good practise to do it in this way.. (access to global variable)
                    Level 1

                    Sounds to me you have to define many global variables. See this page for some code to solve your problem:

                     

                    http://www.davidtucker.net/2007/10/07/getting-started-with-cairngorm-%E2%80%93-part-1/

                     

                    You can define all your globals in the model locator that you can then import into any of your components. Note that you don't really need to use Cairngorm - the article explains how, quoting ...

                     

                    "This code may look a bit daunting in the beginning, but trust me that it is not as difficult as it may appear. First, we have our package definition and we import some classes. Right now we know that we will need the IModelLocator interface. To use this you will need the Cairngorm SWC that can be found here: Cairngorm. Also note - you could build a model locator without Cairngorm, and I do this frequently on small projects (you just leave out the 'implements IModelLocator' and 'import com.adobe.cairngorm.model.IModelLocator' code from the Model Locator)."

                    • 7. Re: Is it a good practise to do it in this way.. (access to global variable)
                      TodLoch Level 1

                      You are right. This article is very useful. I did as the author said, and it works.

                      This is the solution that I needed. Now I can store in one place all variables (which doesn't change in time, but are loaded from MySQL database) and use it in many controls and components.

                       

                      I have to look closer to Cairngorm, it seems to be useful framework.

                       

                      Many thanks!!!