6 Replies Latest reply on Mar 11, 2010 8:51 AM by uthrav

    Sharing variables among mxml files

    vkaushal21

      I have two mxml files..

       

      Say file1.mxml and file2.mxml

       

      I have a class class1 in file "action_script.as"

       

      Problem is, I have an object obj_class1 of class class1 in file1.mxml. I want to use the same object obj_class1, created in file1.mxml in file1.mxml.

       

      I don't know how to share the variables / cdata across the mxml files.

        • 1. Re: Sharing variables among mxml files
          archemedia Level 4

          The state of the art solution for this is to use a singleton class. There are many articles on this.

           

          An example:

           

          Create a class like this:

          this class has a method getInstance which returns an instance of the class GlobalVariables (or whatever name you choose)

          By calling this method, you're sure that there is only one instance of this class througout your application. By adding properties to this class, you can share them anywhere.

           

          package myPackage
          {
             
              [Bindable]
              public class GlobalVariables
              {
                     
                  private static var _instance:GlobalVariables;
                 
                 
                 
                  public function GlobalVariables()
                  {
                  }
                 
                  public static function getInstance():GlobalVariables
                  {
                      if (GlobalVariables._instance == null)
                      {
                          GlobalVariables._instance = new GlobalVariables();
                      }
                      return GlobalVariables._instance;
                  }
                 
                  public static function deleteInstance():void
                  {
                      if (GlobalVariables._instance != null)
                      {
                          GlobalVariables._instance = null;
                      }
                  }
                 
                  //PROPERTIES
                 
                 
                  //myGlobalVar
                  private var _myGlobalVar:String;
                  public function get myGlobalVar():String
                  {
                      return _myGlobalVar;
                  }
                  public function set myGlobalVar(value:String):void
                  {
                      _myGlobalVar = value;
                  }
                 
                 
              }
          }

           

           

           

           

          Now, how do you store and retrieve these properties:

           

          Store:

          GlobalVariables.getInstance().myGlobalVar = "The value of this property";

           

          Retrieve:

          myLabel.text = GlobalVariables.getInstance().myGlobalVar;

           

           

          It's really very simple. Just adapt to this design pattern and you'll be a happier developer from now on

           

          Dany

          • 2. Re: Sharing variables among mxml files
            John Hall Level 4

            Just wanted to clarify - are you looking to share the data that resides in the class (like a static class) or just want  to be able to create new instances of a class object? As pointed out, the Singleton is one approach if you're sharing data (no need to get into others right now) but maybe you're just trying to have a special object available in all mxml files such as a special extended textInput field or something, in which case simply importing the class could do the trick.

             

            So was the Singleton the type of answer you were looking for?

            • 3. Re: Sharing variables among mxml files
              vkaushal21 Level 1

              Thanks for your replies. Singleton is the answer I was looking for.

              • 4. Re: Sharing variables among mxml files
                uthrav

                Hi Archimedes,

                 

                I have tried both singleton and global variables using public var varname and Application.application.varname; my variable keeps getting set to null across mxml components. i.e. I set value in one component, it works within the component; when I user navigatetourl and move away from the component, the variable is gone; its null again whether I get it from a singleton or using Application.application.varname to retrieve the value Please Please help its 4am here

                 

                regards,

                Uthra

                • 5. Re: Sharing variables among mxml files
                  archemedia Level 4

                  How do you store and retreive your variables in a singleton? Can you post an example?

                   

                  Dany

                  • 6. Re: Sharing variables among mxml files
                    uthrav Level 1

                    Hi Dany,

                     

                    Thanks for your help. I think I have to change the architecture/design of the application. Pls read on

                     

                     

                    Basically, I have around 40 forms and  4 mxml application between them. Do you think this is why the singleton is reinitialized? I am trying to share a global variable set at authentication time across the 4 applications. So really, I should have used the word 'application' instead of 'component' yesterday.

                     

                     

                    Here is the scenario:

                     

                    I had an adapted singleton but then I used a word for word copy of yours hoping it would work. Here is the singleton class:

                     

                    package

                     

                    {

                     

                     

                     

                        [Bindable]

                     

                        public class GlobalVariables

                     

                        {

                     

                              

                     

                            private static var _instance:GlobalVariables;

                     

                          

                     

                          

                     

                          

                     

                            public function GlobalVariables()

                     

                            {

                     

                            }

                     

                          

                     

                            public static function getInstance():GlobalVariables

                     

                            {

                     

                                if (GlobalVariables._instance == null)

                     

                                {

                     

                                    GlobalVariables._instance = new GlobalVariables();

                     

                                }

                     

                                return GlobalVariables._instance;

                     

                            }

                     

                          

                     

                            public static function deleteInstance():void

                     

                            {

                     

                                if (GlobalVariables._instance != null)

                     

                                {

                     

                                    GlobalVariables._instance = null;

                     

                                }

                     

                            }

                     

                          

                     

                            //PROPERTIES

                     

                          

                     

                          

                     

                            //myGlobalVar

                     

                            private var _myGlobalVar:String;

                     

                            public function get myGlobalVar():String

                     

                            {

                     

                                return _myGlobalVar;

                     

                            }

                     

                            public function set myGlobalVar(value:String):void

                     

                            {

                     

                                _myGlobalVar = value;

                     

                            }

                     

                          

                     

                          

                     

                        }

                     

                    }

                     

                     

                       

                     

                    Scenario(Sorry I cannot post full code):

                     

                    application1.mxml (an mxml application; also set in flex builder as default mxml application)


                    var logininfo:GlobalVariables;   
                    private function viewResult(event:ResultEvent)
                    {
                    //Some code
                    if (res[0] == '772')
                       {
                           logininfo = GlobalVariables.getInstance();
                           logininfo.myGlobalVar = username.text;
                        }

                     

                    }

                     

                    Then, in another set of forms ie. another application (anotherform.mxml) whose auto generated html file gets called from the one above via navigateToURL(new URLRequest("anotherform.html"),"_self"), I try to access this set value via a creationComplete handler on the application object

                     

                    [Bindable]
                    var userEmail;

                     

                    public function onCreationComplete():void{
                           
                        userEmail = GlobalVariables.getInstance().myGlobalVar;
                           
                           
                           
                        }

                     

                    Voila, its always null....

                     

                    Pls share your thoughts and ideas about a better design if the issue is with the multiple application design.

                     

                    regards and thanks again
                    uvio