8 Replies Latest reply on Apr 11, 2010 6:49 AM by arunkumard87

    Global Varible

    arunkumard87

      Hai all,

       

      I have a loign page, after user logged.i want to store all the logged in user details in one public arraycollection variable. i want to use the user details throw out the project. how to do that??

       

      Thanks in Advance..

       

      Regards,

      Arun Kumar

        • 1. Re: Global Varible
          kubens Level 2

          Main Application:

          ...

          <fx:Script>

          <![CDATA[

               [Bindable] public var user:ArrayCollection;

          ]]>

          </fx:Script>

          ...

           

          Module:

          ...

          <fx:Script>

          <![CDATA[

               ...

               trace(FlexGlobals.topLevelApplication.user['firstname'])

               trace(FlexGlobals.topLevelApplication.user['lastname'])

               ...

          ]]>

          </fx:Script>

          ...

          • 2. Re: Global Varible
            daslicht Level 2

            Cool,

            I didnt knew that it is possible like this neither , anytime I needed Globals I used a singleton for that.

            • 3. Re: Global Varible
              GordonSmith Level 4

              You could also declare your own MyGlobals class, declare 'user' as a static var in it, and access it as MyGlobals.user.

               

              Gordon Smith

              Adobe Flex SDK Team

              • 4. Re: Global Varible
                arunkumard87 Level 1

                Thanks for reply,

                 

                     I am using Flex 3, how to assign value to the class (public) variable.

                 

                Regards,

                Arun Kumar

                • 5. Re: Global Varible
                  kubens Level 2

                  GlobalVariables.as

                   

                  package net.woocoom.basics
                  {
                      public class GlobalVariables
                      {

                          public var bar:String = 'foo'
                          public var foo:String = 'bar'
                         
                          private static var _instance:GlobalVariables;

                          public function GlobalVariables(forcer: GlobalVariablesEnforcer){
                              if (_instance || !forcer) {
                                  throw new Error('Singleton Instantiation Error.');
                              }
                          }                     

                          public static function getInstance():GlobalVariables{
                              return _instance || new GlobalVariables (new GlobalVariablesEnforcer());
                          }

                      }
                  }


                  class GlobalVariablesEnforcer {
                     
                  }

                   

                  Inside you MXML

                   

                  Alert.show(GlobalVariables.getInstance().bar,GlobalVariables.getInstance().foo)

                  • 6. Re: Global Variable
                    arunbiji

                    The answer is 'SharedObject'. PFB a sample way of using SO (Shared Object),

                     

                    Create object --> public var <local_SO_name>:SharedObject = SharedObject.getLocal("<global_SO_name>" [, <path_name>]);                                            

                    Setting data    --> <local_SO_name>.data.<data_name> = <data_value>;

                    Getting data    --> <local_object> = <local_SO_name>.data.<data_name>;

                     

                    More details : http://livedocs.adobe.com/flex/3/langref/flash/net/SharedObject.html, http://livedocs.adobe.com/flex/3/html/help.html?content=lsos_3.html

                    • 7. Re: Global Variable
                      arunkumard87 Level 1

                      Thanks for reply,

                       

                      Login page:

                       

                      UserObj.SetDetails(ResArr[0]["id"],ResArr[0]["user_nm"]);

                       

                      Class:

                       

                      public function SetDetails(UserId:int,UserName:String):void
                              {
                                      So= SharedObject.getLocal("UserDtls");
                                      So.data.UserId=UserId;
                                      UID=UserId;
                                      So.data.UserName=UserName;
                                      So.flush(100);
                              }
                              public function GetDetails():int //:ArrayCollection
                              {
                                  So=SharedObject.getLocal("UserDtls");
                                  return So.data.UserId;    
                              }

                       

                      Thanks and regards,

                      Arun Kumar

                      • 8. Re: Global Variable
                        arunkumard87 Level 1

                        Thanks for reply,

                         

                        I tried the below code. but its not working. could any one check and help me.

                         

                        i checked in net . they are saying getLocal will work only when we use https.

                         

                        Please correct the below code or tell some other method to do the same.

                         

                        Login page:

                         

                        UserObj.SetDetails(ResArr[0]["id"],ResArr[0]["user_nm"]);

                         

                        Class:

                        public function SetDetails(UserId:int,UserName:String):void
                                {
                                        So= SharedObject.getLocal("UserDtls");
                                        So.data.UserId=UserId;
                                        UID=UserId;
                                        So.data.UserName=UserName;
                                        So.flush(100);
                                }
                                public function GetDetails():int //:ArrayCollection
                                {
                                    So=SharedObject.getLocal("UserDtls");\
                                    return So.data.UserId;       
                                }

                         

                        Home page:

                        Alert.show(UserObj.GetDetails().toString());

                         

                        <local:LgDtls id="UserObj" />

                         

                        Thanks and Regards,

                        Arun Kumar