2 Replies Latest reply on Apr 2, 2010 4:32 AM by HakkaMc

    FB4 - custom component problem

    HakkaMc

      I created new custom component:

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

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

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

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

      label="universal" width="503" height="570"

      >

      <s:TextArea name="ta" x="10" y="11" editable="false" width="483" height="353"/>

      <s:TextArea name="textToSend" x="11" y="398" width="482" height="164" editable="true"/>

      <s:Button x="327" y="370" label="Send message" id="sendButton"/>

      </s:NavigatorContent>

       

      and added it into the main MXML file into element

       

      <mx:TabNavigator id="myTabNavigator" x="442" y="24" width="503" height="570">

      </mx:TabNavigator>

       

      via ActionScript:

       

      var tComponent:tabComponent = new tabComponent();

      tComponent.id = "chatWindow";

      myTabNavigator.addElement(tComponent);

       

      But now I want to access the button with id="sendButton" from script section in the main MXML. I tried something like

      this["sendButton"] or tComponent.sendButton

      but having no luck.

       

      Is there a way how to access the element packed in custom component? Simply...I need something like document.getElementById("sendButton") and then add a click event listener.

       

      Thanks...

        • 1. Re: FB4 - custom component problem
          prksingh Adobe Employee

          The problem you are facing is probably due to deffered instantiation of children in the TabNavigator.

           

          When you enter an AddChild statement for a container, you generally expect that component is instantly added. Now, consider Navigator containers. A viewStack for example can have any number of children which user may not even navigate to and still they should be given memory?

           

          For this reason, most navigator containers postpone creating children until they need to be displayed. This property can be changed through the creationPolicy property of the navigator container. Making creationPolicy="all" will force all children to be created and rid you of your problem. Here is the code:

           

          MAIN FILE:

          <?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" minWidth="955" minHeight="600" creationComplete="init()">
              <fx:Declarations>
                  <!-- Place non-visual elements (e.g., services, value objects) here -->
              </fx:Declarations>
              <fx:Script>
                  <![CDATA[
                      import Flex.components.tabComponent;
                     
                      import flash.text.engine.TabAlignment;
                     
                      import mx.controls.Alert;
                      protected function init():void{
                          var tComponent:tabComponent=new tabComponent();
                          tComponent.id="chatWindow";
                          myTabNavigator.addElement(tComponent);
                          tComponent.sendButton.addEventListener(MouseEvent.CLICK,handlerMethod);
                      }
                      protected function handlerMethod(event:MouseEvent):void
                      {
                          Alert.show("Handler method called");
                      }
                  ]]>
              </fx:Script>
              <mx:TabNavigator id="myTabNavigator" x="171" y="148" width="200" height="200" creationPolicy="all" >
                 
              </mx:TabNavigator>
          </s:Application>

           

          tabComponent.mxml remains same.

           

          Hope it will help you.

           

           


          1 person found this helpful
          • 2. Re: FB4 - custom component problem
            HakkaMc Level 1

            Thanks,

            I thought something like you about the memory usage and I expected that there have to be something like force creation method.

             

            creationPolicy="all" helps me :-)