8 Replies Latest reply on Jan 2, 2009 11:11 PM by ntsiii

    Local/Global Scope

    eseneca
      Hello,

      I am new to Flex coming from a VB.net background. I am trying to define a class that can be accessed via the main application and any modules or pop-up windows that I may create. I want this for tracking and passing values between the scopes. Is there some guide on how to pass information between modules/popups and the main application?

      Thanks
        • 1. Re: Local/Global Scope
          rtalton Level 4
          Hi,
          If you are thinking of storing variables in a global class, you'll get a lot of feedback from most Flex developers urging you not to. The best practice is to define custom events and listeners (event handlers) to facilitate communication between modules/popups and the main app.
          Say if you have a popup open. When closed, it can dispatch a custom event which can store any information you want it to. Back in the main app, a "listener" handles that event, reading in any data it needs to and taking any necessary actions.
          Check the help docs for great tips about dispatching custom events under the section on custom components:
          http://livedocs.adobe.com/flex/3/html/help.html?content=mxmlcomponents_advanced_4.html

          Here's a simple example to get you started:
          http://www.iotashan.com/index.php/2008/03/19/shans-simple-examples-using-events-aka-how-to -pass-data-from-components/

          Happy Flex Year!
          • 2. Re: Local/Global Scope
            eseneca Level 1
            Thanks for the reply, I think I understand what is going on via the events. Let me give that a try...
            • 3. Local/Global Scope
              eseneca Level 1
              I see how to make the application pass information. Do you have an example of passing information back from the popup window to the application? Do I add an eventListener to the main application?
              • 4. Re: Local/Global Scope
                rtalton Level 4
                Your strategy depends on what you need to pass back to the app. Flex events have lots of built-in properties which include information about parts of your component and its data.
                Give us an idea of what you need to retrieve, and you'll get lots of suggestions on how to approach it.
                • 5. Re: Local/Global Scope
                  eseneca Level 1
                  For now, I have a string variable in the main application. I have built the popup window based on the example you presented to me. Now I do an XML call to get back some data from a simple webservice. I have parsed that data and assigned it back to the string var in the TitleWindow. I unload the Title window to return to the main application, but I cannot seem to figure out how to access the assigned var.

                  • 6. Re: Local/Global Scope
                    rtalton Level 4
                    That should be easy, here's one way:
                    In your pop-up Title Window custom component (let's just call it "MyPopper"), declare a public var:
                    public var myPopperInformationString:String;

                    Now, define a Metadata tag with a simple custom event defined, like:
                    <mx:Metadata>
                    [Event(name="CloseMyPopperEvent", type="flash.events.Event")]
                    </mx:Metadata>
                    (Place this above your <mx:Script> tag, not in the script tags)

                    Your done with MyPopper. It now has a new public var and a custom event defined.

                    Back to the main app:
                    (You said you already have a string var in the main app):
                    private var myMainAppStringVar:String;
                    And you (hopefully) already have declared a var for the new PopUp, also within the Script block:
                    private var myPopper:MyPopper;//The popup.

                    When you run ActionScript code to open MyPopper, add a listener for your custom event:
                    private function openThePopper():void{
                    myPopper = new MyPopper;//The new pop-up window.
                    myPopper.addEventListener("CloseMyPopperEvent",closePopperHandler);//The handler.
                    PopUpManager.addPopUp(myPopper,this,true);//Open it.
                    PopUpManager.centerPopUp(myPopper);//Center it.
                    }

                    Now, you need the handler you just declared in the listener:
                    public function closePopperHandler(event:Event):void{
                    ...do stuff here...
                    }

                    And finally, add code to the handler to grab the data from myPopper:
                    public function closePopperHandler(event:Event):void{
                    myMainAppStringVar = myPopper.myPopperInformationString;//You got it! Yay!
                    //Let's clean up now:
                    //Remove any references to the pop-up so the garbage collector can destroy it later:
                    myPopper.removeEventListener("CloseMyPopper",closePopperHandler);
                    PopUpManager.removePopUp(myPopper);
                    myPopper = null;
                    }

                    So this is one way. Another way would be to create a custom event with custom properties in an AS file and use that anywhere in your app. But if you're just grabbing simple strings/values from MyPopper, no need to re-invent the wheel. Just pass it back to vars in the main app in a close handler.
                    Please read the help docs under "Custom Components" to understand what's happening with all the code above, so you can do things your own way--what makes sense to you.
                    My apologies if there are any typos. I didn't test this code.
                    • 7. Re: Local/Global Scope
                      *gsb* Level 1
                      Yet another way...
                      would be to use "dependency injection" ( or an observer pattern ) to give the popup a reference to your main data manager. Have variables/setters with appropriate bindings defined in the data manager class to hold the "popup's state data" which the popup sets via the interface to the main data manager.

                      Data bindings in the main data manager will notify the main application of the state data changes as needed.

                      • 8. Re: Local/Global Scope
                        ntsiii Level 3
                        Another tip with events is that almost all of the event objects have the target and currentTarget properties, which provide a reference to the component instance that dispatched the event. So rtalton's code could be changed to this:
                        myMainAppStringVar = event.target.myPopperInformationString;//You got it! Yay!

                        Tracy