10 Replies Latest reply on Oct 5, 2009 7:16 PM by Muzak

    how do I add a global variable to my app?

    xandesign Level 1

      Hi all,

       

      I am building a flex app which displays data in different tabs from a mysql database and I wan't to add a sign in process to make sure only registered users of the mysql db can see the data.

       

      To achieve this I built a sign in component which accepts a username, password and the name of the database to connect to. My flex app then uses a php script to verify that the user can connect to the database using the details entered. If not, the sign in component displays an error message, but if sign in details are verified then the user can see the data from the database.

       

      How can I store the details in the flex app for that users session so that when the user queries the db for data, flex uses the details entered during the sign in process to connect to the mysql db and submit the query?

       

      I thought about storing the details as an array in a global variable and then accessing that global variable from anywhere in my app but I don't know if that is the correct/secure way to do what I want, nor do I know how to do it. Any help would be much appreciated.

       

      Thanks in advance,

       

      xander

        • 1. Re: how do I add a global variable to my app?
          Gregory Lafrance Level 6

          My Flex 3 Cookbook post shows how to use custom events to control login in this manner:

           

          http://cookbooks.adobe.com/index.cfm?event=showdetails&postId=15466

           

          If this post answered your question or helped, please mark it as such.

          • 2. Re: how do I add a global variable to my app?
            Matt Le Fevre Level 4

            quick, dirty example, could be improved on but it'll get you rolling;

             

            After user login has succeeded, send off another HTTP service eg;

             

            ========

             

            <mx:Script>
                     <![CDATA[


                        public var whateverUserNameWasEntered:String;

                         public var whateverUserPasswordEntered:String;


                 public function onLoginSuccess():void
                 {

                      whateverUserNameWasEntered:String = loginbox.text;

                      whateverUserPasswordEntered:String = passwordbox.text;


                      storeSessionStuff.send();

                 }


                 ]]>
                </mx:Script>


            <mx:HTTPService id="storeSessionStuff" showBusyCursor="true" method="POST"  url="http://localhost/store.php" useProxy="false">


                    <mx:request xmlns="">   
                      <username>
                           {whateverUserNameWasEntered}
                      </username>

                      <password>

                           {whateverUserPasswordEntered}

                      </password>       
                    </mx:request>


                </mx:HTTPService>     
                

             

            ========

             

            direct it to the php file.....

             

            <?php


            session_start();


            $_SESSION['username'] = $_POST['username'];
            $_SESSION['password']   = $_POST['password'];


            ?>

             

            Then, whenever you want to get the information out of the Session, send off another HTTP Service...;

             


            <mx:HTTPService id="getSessionStuff" result="storeData(event)" showBusyCursor="true" method="POST"  url="http://localhost/get.php" useProxy="false"/>

             

            to the php file....


            <?php


            session_start();


            print "<details><username>".$_SESSION['username']."</username><password>".$_SESSION['password'] ."</password></details>";


            ?>

             


            which, you need a result function for:

             


            <mx:Script>
                     <![CDATA[


                      private function storeData(evt:ResultEvent):void
                      {
                            var username:String;

                            var password: String;


                           username = evt.result.details.username;    

                           password = evt.result.details.password;         
                           
                      }


                  ]]>
            </mx:Script>

             

             

             

            ta daa

             

            (if thats what you meant)

            • 3. Re: how do I add a global variable to my app?
              xandesign Level 1

              I want to store the details within my flex application and not within php sessions. Can this be done?

               

               

              xander

              • 4. Re: how do I add a global variable to my app?
                Matt Le Fevre Level 4

                Oh, apologies i got confused when you used the word session.

                 

                 

                Yes it's entirely possible,

                 

                as you've already mentioned, you can declare global variables and store them in there.

                 

                 

                Or, if you do want it more secure, declare them privately and then only pass it to the functions that require the value

                • 5. Re: how do I add a global variable to my app?
                  xandesign Level 1

                  I thought that was the way to do it but I just don't know how to do it. Can you tell me how I can do this?

                   

                  Thanks,

                   

                  xander

                  • 6. Re: how do I add a global variable to my app?
                    Gregory Lafrance Level 6

                    I still think custom events are the way to go.

                     

                    I gave an example of login, but you can do that with any private data. Components with a need to know will listen for the events and handle them, accessing the data.

                     

                    If this post answered your question or helped, please mark it as such.

                    • 7. Re: how do I add a global variable to my app?
                      xandesign Level 1

                      Will the custom event store the data added to it from the login form for the entire time the user has the webpage, with the flex app open?

                      • 8. Re: how do I add a global variable to my app?
                        Gregory Lafrance Level 6

                        No, you use the custom event to update the global variable from the login form.

                         

                        Now that I think about it, in this case custom events are a way to update the global variable from the login form, and then you can access the global variable using the model on MVC architecture.

                         

                        http://cookbooks.adobe.com/post_Building_a_wizard_using_a_simplified_MVC_architect-11246.h tml

                         

                        If this post answered your question or helped, please mark it as such.

                        • 9. Re: how do I add a global variable to my app?
                          wolfman33 Level 1

                          The easiest way I know of doing this is to use a model locator.

                           

                          create a class mainAppModelLocator.as

                           

                          package
                          {
                              import mx.collections.ArrayCollection;
                             
                             
                              [Bindable]
                              public class mainAppModelLocator {
                                 
                                  // Single Instance of Our ModelLocator
                                  private static var instance:mainAppModelLocator;
                                 
                                  public function mainAppModelLocator(enforcer:SingletonEnforcer) {
                                     
                                  if (enforcer == null) {
                                          throw new Error( "You Can Only Have One ModelLocator" );
                                      }
                                  }
                                 
                                  // Returns the Single Instance
                                  public static function getInstance() : mainAppModelLocator {
                                     
                                      if (instance == null) {
                                          instance = new mainAppModelLocator( new SingletonEnforcer );
                                      }
                                     
                                      return instance;
                                  }
                                 //DEFINE YOUR VARIABLES HERE

                           

                                  public var session:Number;

                           

                              }
                          }
                          // Utility Class to Deny Access to Constructor
                          class SingletonEnforcer {}

                           

                          Then you can change and access your "session var" from any component or file by simply including this line on that page:

                           

                          import mainAppModelLocator;

                          [Bindable] private var sessionVar:mainAppModelLocator = mainAppModelLocator.getInstance();

                           

                          and access the actual session var like this: sessionVar.session

                           

                           

                          You can put as many "session" variables of any type as you want in the model locator. It makes it very convenient. And you can use binding and won't have to worry about passing data using events, except when you really need to.

                           

                          I hope this helps.

                          • 10. Re: how do I add a global variable to my app?
                            Muzak Level 3

                            Just a tip: don't pick up bad habits such as calling a Model a ModelLocator like Cairngorm does

                            The class doesn't "locate" Models, it "is" the Model.

                             

                            And start a class with an uppercase character:

                             

                                 public class mainAppModelLocator

                             

                            becomes:

                             

                                 public class ApplicationModel

                             

                            Other than that, using a Singleton is what you're looking for as wolfman explained.

                             

                            ps:

                            ARP is/was the MVC framework that actually had and used a ModelLocator, Cairngorm took over the naming, but not its implementation.

                            If you're interested in the details, google:

                            • ARP ModelLocator
                            • APRX ModelLocator
                            • ARP Christophe Herreman