2 Replies Latest reply on Nov 18, 2009 1:48 AM by mewk

    An error occurs when the currentstate of child component is changed.

    tori0915

      Hi!!

       

      An error occurs when the currentstate of child component is changed.
      the currentstate is changed as follows and do it as I got advice in this forum before.

       

      construction of files as follows:

      tmp_main.mxml (states:login, menu, submenu, detail)

         tmp -- tmp_header.mxml

               -- tmp_menu.mxml

               -- tmp_login.mxml

               -- tmp_submenu.mxml

               -- tmp_detail.mxml (states:body1,body2)

       

      tmp_submenu has two button which are to tmp_detail:body1 and to tmp_detail:body2.

      An error occurs when I click the button to tmp_detail:body2.

       

      Could you tell me the problem in my source?

       

      I attach all samples source.

       

       

      -----tmp_submenu.mxml(child)----------------------------

      <?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/halo" width="400" height="300" xmlns:tmp="tmp.*">

      <fx:Script>
        <![CDATA[
        
         import tmp.tmp_detail;
        
         protected function button1_clickHandler(event:MouseEvent):void
         {
          parentApplication.currentState = "detail";
         }
        
         protected function button2_clickHandler(event:MouseEvent):void
         {
          parentApplication.currentState = "detail";
          parentApplication.tmp.tmp_detail.currentState = "body2";   }
        
        ]]>
      </fx:Script>
       
      [...]


      <s:Button label="to detail_body1" enabled="true" click="button1_clickHandler(event)" x="26" y="73"/>
      <s:Button label="to detail_body2" x="26" y="102" enabled="true" click="button2_clickHandler(event)"/>

      </s:Group>

      --------------------------------------

      Thank you for your help...

        • 1. Re: An error occurs when the currentstate of child component is changed.
          tori0915 Level 1

          Sorry,I attached all samples source again.

          • 2. Re: An error occurs when the currentstate of child component is changed.
            mewk Level 3

            tori0915,

             

            you cannot access

            • <tmp:tmp_detail includeIn="detail" />
              

            with

            • parentApplication.tmp.tmp_detail;
              

             

            instead give the component an id and access through the child component through this. something like:

            • <tmp:tmp_detail id="detailComp" includeIn="detail" itemCreationPolicy="immediate"/>
              
            • parentApplication.detailComp;
              

             

            Also you may run into trouble because of the includeIn property -- "detailComp" object will only be created once the "detail" state has been used and until then the object doesn't exist so you will get errors if you try to access properties on a null object. You can get around this by using itemCreationPolicy="immediate"

             

            Lastly, a few tips

            • use friendlier naming scheme. <tmp:tmp_detail /> isn't so hot, better is <comp:DetailGroup /> or something...
            • I'm not a big fan of child components communicating in the way you have set up. Adobe recommends dispatching events and frameworks employ other methods. Accessing components through the parentApplication is easy and possibly executes faster but leads to a tightly coupled system that will cause more trouble down the road.

             

            - e