8 Replies Latest reply on Jul 2, 2006 12:05 AM by doug777

    How to refer to component

    doug777 Level 1
      Why doesn't this code compile:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*">

      <mx:Script>
      <![CDATA[
      stxt.text = "abc";
      ]]>
      </mx:Script>

      <mx:Text id="stxt" text="" width="240" height="650"/>

      </mx:Application>

      The problems panel reports 'Access of undefined property stxt.'

      Doug
        • 1. Re: How to refer to component
          doug777 Level 1
          I now realize this has to be inside a function, but this still doesn't solve my problem.

          If I set up a component in Main.mxml e.g.

          <mx:Text id="stxt" text="" width="240" height="650"/>

          How can I set the text in an imported class file e.g.

          var txtblk = Main.stxt;//What should this line be?
          txtblk.text = "abc";

          Doug
          • 2. How to refer to component
            njadobe
            Hi Doug,

            There are a few ways to accomplish what I think you're trying to do. By "imported class file", I assume you mean a separate MXML file that defines a component you're using in your main application--for example, you have MyComponent.mxml, you have a <MyComponent> tag in your application, and you want MyComponent to be able to access members of the application.

            The quickest way to do this is to access the member directly from the subcomponent using Application.application:

            var txtblk: Text = Application.application.stxt;
            txtblk.text = "abc";

            This works, but it isn't the cleanest way to code--in general, you should avoid "upward dependencies" where components have to know about the things that contain them.

            A better way to accomplish this would be to expose a bindable property from your component, then bind to that from the text field in the outer application. Changing the property value in the component would then automatically update the text field in the application.

            For example, suppose you wanted clicking a button in MyComponent to update a text field in the main app. MyComponent.mxml would contain:

            ...
            <mx:Script>
            [Bindable]
            public var textValue: String;
            </mx:Script>
            <mx:Button click="textValue= 'new text';"/>
            ...

            And the main application would contain:

            ...
            <MyComponent id="myComponent"/>
            <mx:Text text="{myComponent.textValue}"/>
            ...

            Hope this helps.

            nj
            Flex Builder team
            • 3. Re: How to refer to component
              doug777 Level 1
              Thanks so much for your help, but I still have a problem.

              I want to access a Text component id="stxt" in Main.mxml from MyClass in mypackage.

              In Main.mxml: import mypackage.MyClass;

              A RemoteObject call in MyClass returns data to resultHandler:

              private function resultHandler(event:ResultEvent):void{
              var mydata = event.result;
              Alert.show( ObjectUtil.toString(mydata) );//shows correct returned data
              var txtblk:Text = Application.application.stxt;
              txtblk.text = ObjectUtil.toString(mydata);
              mx.managers.CursorManager.removeBusyCursor();
              }

              Returns the error: Access of undefined property Application.

              If I use Bindable on static variable mydata, stxt.text ends up with the null object from the definition:
              public static var mydata:Object = {};
              i.e. global variable mydata is not updated by the change to mydata which occurs in resultHandler.

              Is there a way to solve this?

              Doug
              • 4. Re: How to refer to component
                doug777 Level 1
                Sorry, the first line in resultHandler is actually: mydata = event.result; - there is no var.

                Doug
                • 5. Re: How to refer to component
                  ur_dtrain Level 1
                  import mx.core.Application;
                  • 6. Re: How to refer to component
                    chrisichris Level 1
                    quote:


                    In Main.mxml: import mypackage.MyClass;

                    A RemoteObject call in MyClass returns data to resultHandler:

                    private function resultHandler(event:ResultEvent):void{
                    var mydata = event.result;
                    Alert.show( ObjectUtil.toString(mydata) );//shows correct returned data
                    var txtblk:Text = Application.application.stxt;
                    txtblk.text = ObjectUtil.toString(mydata);
                    mx.managers.CursorManager.removeBusyCursor();
                    }

                    Returns the error: Access of undefined property Application.



                    Try to cast the Application.application to you application:

                    var txtblk:Text = Main(Application.application).stxt;

                    Maybe this works (You also have to import the Main)

                    For the static part: I think it is better if you don't use static vars too often. Maybe it is better if you create a new instance of MyClass in Main and than pass Main into this class (That's at least how I do it currently - note I am not very experienced with Flex/Flash):

                    <mx:application ... creationComplete="initAppp()">
                    [Bindable]
                    public var myClass:MyClass;
                    private function initApp():void {
                    myClass = new MyClass(this);
                    }
                    </mx:application>

                    class MyClass {
                    private var main:Main;
                    public function MyClass(m:Main) {
                    main = m;
                    }
                    .....
                    }

                    Than use the bindings in Main {myClass.anyBindableProp} and in MyClass you do not have to call Application.application.
                    • 7. Re: How to refer to component
                      doug777 Level 1
                      Many thanks to everyone. The key is to import mx.core.Application. Then it works immediately.

                      But I do like chrisichris's idea to avoid the static variable. Binding the class instance may be very powerful if it means I can just set and read directly any of the components in the Main page. I'm testing it out now to see what limitations there are.

                      Your help is much appreciated,
                      Doug
                      • 8. Re: How to refer to component
                        doug777 Level 1
                        Yes this is brilliant.

                        Forget about static variables and Application.application, using chrisichris's method you can just do everything !!

                        Thanks so much !!

                        Doug