4 Replies Latest reply on Jul 9, 2007 7:29 AM by laapsaap

    I have coded a class "example.as" and I want to append text in the textarea that is on main.mxml. How do I access it?

    laapsaap
      Hi,
      I am trying to something simple, i have browsed this whole forum but cant find my answer. Hope you guys can help, thx.

      In my main.mxml I have added a textarea with id "debug".
      Now I have coded a class "example.as".

      I want append text into the textarea with id "debug" in example.as, I cant seem to find a way to do it. Can anyone help me out please?

      Thx
        • 1. Re:  I have coded a class "example.as" and I want to append text in the textarea that is on main.mxml. How do I access it?
          peterent Level 2
          The best-practice way to do this is use an event from your example class which is then picked up by the main application:

          <local:example debugText="{debug.text=event.text}" ... />

          First define a class called DebugTextEvent which extends flash.events.Event. It should have a public var text member.

          In the examle.as class, declare the event in metadata:

          [Event(name="debugText",type="DebugTextEvent")]

          In the example as class, dispatch the event:

          var evt:DebugTextEvent = new DebugTextEvent();
          evt.text = "whatever you want here";
          dispatchEvent( evt );

          You need to fill in some of the blanks and figure out how to make the event class, but that's the general idea. Now that probably appears to be complicated. And yes, it is, but the value is that you've make your application more flexible in that you can re-use the event class or change the structure of your app with minimal changes.

          Now here's the shortcut: from within example.as

          parentApplication.debug.text = "whatever you want here";

          That's it - a single line of code. I would do the first, longer way, because it fits in with the Flex framework, but sometimes you need to do something straightforward.

          • 2. Re:  I have coded a class &quot;example.as&quot; and I want to append text in the textarea that is on main.mxml. How do I access it?
            bitwyse Level 1
            I just did the same exact thing last week. First, I used the event way to dispatch the event from my component to the parent mxml file. And it worked etc.

            While I was reviewing the code I saw the line that used parentApplication and I thought why not just use that to access any object from the parentApplication.


            So I created a test and it works great. Why would you not do that always? What is so great about all that extra code using events. Why not just access the parrentApplication object. Can you give some concrete examples why one is better than the other?

            Thanks.
            • 3. Re:  I have coded a class &quot;example.as&quot; and I want to append text in the textarea that is on main.mxml. How do I access it?
              peterent Level 2
              In the situation you have here, doing the direct approach is probably the most appropriate. But suppose you move the TextArea field from the main Application into another component? How would you find it. Using events makes it easier to decouple the parts and move them around as your design changes, plus it allows you to more easily reuse a component in other applications.

              In this case, parentApplication works fine. Suppose the component is so useful that you want to use it in another app?

              That's why I started the 'best-practice' solution first.
              • 4. I have coded a class &quot;example.as&quot; and I want to append text in the textarea that is on main.mxml. How do I access it?
                laapsaap Level 1
                Hi Peter,

                Thanks for your replies. I fully understand what you are trying to say. I am new to flex, learning new things everyday.

                I am trying both of your solutions, the dirty and easy way and the DebugTextEvent way.

                There's just something simple that stop me from using;
                parentApplication.debug.text = "new value"; in example.as

                This error message I get;

                TypeError: Error #1009: Cannot access a property or method of a null object reference.
                at mx.core::UIComponent/get parentApplication()
                at com.socket::example/private::check1()
                at com.socket::example/private::check2()

                The TextArea "debug" is on the main.mxml for sure and I even defined creationPolicy="all" in the TabNavigator where the TextArea is in. Any idea why it gives me an 1009 error?

                Thanks