Skip navigation
nilamk
Currently Being Moderated

WindowedApplication-get ID

Dec 22, 2011 10:20 PM

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

 
Replies
  • Currently Being Moderated
    Dec 22, 2011 11:03 PM   in reply to nilamk

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 23, 2011 8:40 AM   in reply to nilamk

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 23, 2011 1:56 PM   in reply to DonMitchinson

    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>

     

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 23, 2011 4:31 PM   in reply to John Hall

    Nice ! Great example.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 25, 2011 9:26 PM   in reply to nilamk

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 25, 2011 9:46 PM   in reply to drkstr_1

    Bt it can't work.

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 6, 2012 7:32 AM   in reply to nam1490

    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)}"/>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points