9 Replies Latest reply on Jan 6, 2012 7:32 AM by DonMitchinson

    WindowedApplication-get ID

    nilamk

      How to get an ID or a variable from the WindowedApplication to the Component?

        • 1. Re: WindowedApplication-get ID
          DonMitchinson Level 2

          Not sure exactly what you're trying but possibly

          outerApplication.variablename may work if you've declared the variable public.

           

          Another way is to declare a component variable as public

          and then set it to the Application var value when you're opening the component.

          I've used this with Popup components.

          • 2. Re: WindowedApplication-get ID
            nilamk Level 1

            thanks for reply.

            but again i want ID from Window to component.

            here is my .mxml file-->

             

            <mx:Window

                      xmlns:mx="http://www.adobe.com/2006/mxml"

                      layout="absolute"

              creationComplete="onCreationComplete()"

                      >

            <mx:Button x="100" y="70" label="Ok" click="controller.Button_clickHandler(event)"/>

                      <mx:Label x="29" y="43" text="New Folder:"/>

                      <mx:TextInput  id="textIP" x="114" y="42"/>

              </mx:Window>

             

            And my ActionScript file is-->

            public class AppController

                      {

                     public function Button_clickHandler(event:MouseEvent):void

                                          {

                                //here how to get id "textIP" defined in .mxml file.

                        }

            }

            • 3. Re: WindowedApplication-get ID
              DonMitchinson Level 2

              That doesn't look like code that works.

              Soemthing that can be copied/pasted into a basic project would be more useful.

               

              Do you have something that has a basic Alert to show the button has been clicked

              or are you starting from scratch with just an idea?

              • 4. Re: WindowedApplication-get ID
                John Hall Level 4

                If you have to rely upon it a lot, you best investigate the event system a bit more or look at frameworks like Robotlegs or Mate. However, for a quick one-off, you can use FlexGlolbals.topLevelApplication.myVariable just about anywhere, if the main class has that variable publicly defined.

                 

                Here's my amazing app:

                 

                 

                <?xml version="1.0" encoding="utf-8"?>
                <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                                                 xmlns:s="library://ns.adobe.com/flex/spark"
                                                 xmlns:mx="library://ns.adobe.com/flex/mx"
                                                 xmlns:ns1="*"
                                                 width="300" height="300" minWidth="300" minHeight="300" creationComplete="init()">
                          <s:layout>
                                    <s:BasicLayout/>
                          </s:layout>
                          <fx:Script>
                                    <![CDATA[
                  
                                              public var myVariable:String;
                  
                                              private function init():void{
                                                        myVariable = "Hello World";
                                              }
                                    ]]>
                          </fx:Script>
                          <ns1:myComponent horizontalCenter="0" verticalCenter="-31">
                          </ns1:myComponent>
                </s:Application>
                
                
                

                 

                and my equally cool component

                 

                 

                <?xml version="1.0" encoding="utf-8"?>

                <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"

                                     xmlns:s="library://ns.adobe.com/flex/spark"

                                     xmlns:mx="library://ns.adobe.com/flex/mx">

                          <fx:Script>

                                    <![CDATA[

                                              import mx.controls.Alert;

                                              import mx.core.FlexGlobals;

                                    ]]>

                          </fx:Script>

                          <fx:Declarations>

                                    <!-- Place non-visual elements (e.g., services, value objects) here -->

                          </fx:Declarations>

                          <s:Button x="0" y="0" label="Say Hello" click="{Alert.show(FlexGlobals.topLevelApplication.myVariable)}"/>

                </s:Group>

                 

                • 5. Re: WindowedApplication-get ID
                  DonMitchinson Level 2

                  Nice ! Great example.

                  • 6. Re: WindowedApplication-get ID
                    nilamk Level 1

                    Thanx a lot.

                    Bt this was work only in case of flex 4 and above version.What i have to use in case of flex 3.4?

                    • 7. Re: WindowedApplication-get ID
                      drkstr_1 Level 4

                      Thanx a lot.

                      Bt this was work only in case of flex 4 and above version.What i have to use in case of flex 3.4?

                       

                       

                      Swap out FlexGlobals.topLevelApplication with Application.application, and I think you need to cast it as your main class as well EG: MyApp(Application.application).someVar

                      • 8. Re: WindowedApplication-get ID
                        nam1490 Level 1

                        Bt it can't work.

                        get the error as property textIP not found and there is no default value.

                        • 9. Re: WindowedApplication-get ID
                          DonMitchinson Level 2

                          I believe it will work if you use a two-way public variable (if that's in 3.4???) in the value attribute of your text component.

                          But you need to reference the 'variable name' (as in the provided example) not the text component id.

                           

                          So you'd change your text component declaration to

                           

                          <mx:TextInput  id="textIP" text=@"{myvariable}" x="114" y="42"/>

                           

                           

                          and still use the button call

                          <s:Button x="0" y="0" label="Say Hello" click="{Alert.show(FlexGlobals.topLevelApplication.myVariable)}"/>

                           

                          or for 3.4

                          <s:Button x="0" y="0" label="Say Hello" click="{Alert.show(Application.application.myVariable)}"/>