8 Replies Latest reply on Dec 31, 2010 2:28 AM by kiran7881

    Is Parent (Application) is it possible to access child components??

    kiran7881 Level 1

      Hi ,

       

      In my  MXML Application , is it possible for me to access the child components Data

       

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">
      <mx:Script>
          <![CDATA[
              public function CallMe():void
              {
      // Here i want to access the Canvas TextInput fromCanvas id
              }
          ]]>
      </mx:Script>
          <mx:TextInput x="113" y="128" id="kiran" change="CallMe()"/>
          <local:CC>
          </local:CC>
      </mx:Application>

       

      =================================================

       

      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" backgroundColor="black">
          <mx:TextInput id="fromCanvas" x="83" y="85"  text="From Canvas"/>
         
      </mx:Canvas>

        • 1. Re: Is Parent (Application) is it possible to access child components??
          flex2008 Level 3

          Yes,you can.Use like ccCanvas.fromCanvas,where ccCanvas is the id of CC.But I would not recommend using that approach.Why because,if you use it like that there will exist a dependancy between fromCanvas and your main application.What I would rather do is define a public getter in CC and that getter would return the fromCanvas text

           

          Use this in your CC:

           

          public function get fromCanvasText():String
          {
              return fromCanvas.text;
          }
          
          

           

          And then in your main application:

           

          public function CallMe():void
          {
              trace(ccCanvas.fromCanvasText);
          }


          Again,I would say,use this approach only if you are to access data of immediate children only.For other cases,where you need to share data between parent and grandchildren or between components,I recommend making use of events and models(MVC).

           

          Hope you understood what I am trying to say.If not,please let me know.

          • 2. Re: Is Parent (Application) is it possible to access child components??
            kiran7881 Level 1

            Thanks for the response , but unfortunately , its not working .

             

            Its not able to recognize the CC (trace(CC.fromCanvasText);)

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">
            <mx:Script>
                <![CDATA[
                       import mx.controls.Alert;
            public function callMe():void
            {
            trace(CC.fromCanvasText);
            }
                ]]>
            </mx:Script>
                <mx:TextInput x="113" y="128" id="kiran"  />
                <local:CC>
                </local:CC>
            </mx:Application>

             

            =====

             

            This is my Canvas  CC:

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" backgroundColor="black">
            <mx:Script>
                <![CDATA[
            public function get fromCanvasText():String
            {
                return fromCanvas.text;
            }
                ]]>
            </mx:Script>
                <mx:TextInput id="fromCanvas" x="83" y="85"  text="From Canvas"/>
            </mx:Canvas>

            • 3. Re: Is Parent (Application) is it possible to access child components??
              flex2008 Level 3

              Use the id of CC

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">
              <mx:Script>
                  <![CDATA[
                         import mx.controls.Alert;
              public function callMe():void
              {
              trace(cc.fromCanvasText);
              }
                  ]]>
              </mx:Script>
                  <mx:TextInput x="113" y="128" id="kiran"  change='callMe()'/>
                  <local:CC id='cc'>
                  </local:CC>
              </mx:Application>

              • 4. Re: Is Parent (Application) is it possible to access child components??
                kiran7881 Level 1

                Canvas is the root Component so  it cannot have a Id Attribte .

                 

                Anyway , thanks for your help .

                • 5. Re: Is Parent (Application) is it possible to access child components??
                  flex2008 Level 3

                  Give the id for CC in the main application and not in CC.mxml.Check the code in my last post

                  • 6. Re: Is Parent (Application) is it possible to access child components??
                    kiran7881 Level 1

                    Please help to resolve this : I did the same as what you suggested

                     

                    This is my Canvas

                     

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Canvas id="ccCanvas" xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" backgroundColor="black">
                    <mx:Script>
                    <![CDATA[
                    public function get fromCanvasText():String
                    {
                    return fromCanvas.text;
                    }
                    ]]>
                    </mx:Script>
                    <mx:TextInput id="fromCanvas" x="83" y="85"  text="From Canvas"/>
                    </mx:Canvas>


                    This is my Application (Main)

                     

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">
                    <mx:Script>
                        <![CDATA[
                            import mx.charts.chartClasses.StackedSeries;
                            import mx.controls.Text;
                            import mx.resources.Locale;
                            import mx.controls.Alert;
                    public function callMe():String
                    {
                       
                        var str:String;
                    str =ccCanvas.fromCanvas.toString(); // Error coming here saying that Access of Undefined  Property ccCanvas
                    return str;
                    }
                        ]]>
                    </mx:Script>
                        <mx:TextInput x="113" y="128" id="kiran" text="{callMe()}"/>
                        <local:FF>
                        </local:FF>
                    </mx:Application>

                     

                    Thanks .

                    • 7. Re: Is Parent (Application) is it possible to access child components??
                      flex2008 Level 3

                      This is what I was talking about:

                       

                      Canvas:

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" backgroundColor="black">
                      <mx:Script>
                      <![CDATA[
                      public function get fromCanvasText():String
                      {
                      return fromCanvas.text;
                      }
                      ]]>
                      </mx:Script>
                      <mx:TextInput id="fromCanvas" x="83" y="85"  text="From Canvas"/>
                      </mx:Canvas>

                       

                      Main

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">
                      <mx:Script>
                          <![CDATA[
                              import mx.charts.chartClasses.StackedSeries;
                              import mx.controls.Text;
                              import mx.resources.Locale;
                              import mx.controls.Alert;
                      public function callMe():String
                      {
                        
                          var str:String;
                      str = cc.fromCanvasText;
                      Alert.show(str)
                      return str;
                      }
                          ]]>
                      </mx:Script>
                          <mx:TextInput x="113" y="128" id="kiran" text="{callMe()}"/>
                          <local:FF id="cc">
                          </local:FF>
                      </mx:Application>