2 Replies Latest reply on Jan 11, 2010 10:26 PM by andreamesser

    Accessing variable in parent of component

    andreamesser

      Hi,

       

      I am trying to access a proerty in a parent module called survey. I want to get acess to an HTTPservice I have called XMLQuestions whose result is in a variable called xQuery.

       

      When I access the variable in the module everything works, but I can't figure out how to access it in my component (the code is below) which is used as a child in the module survey.

       

      Thanks for any help you cna provide.

       

       

      Component code:

       

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

       

        

       

       

      <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="580" height="202" layout="absolute" title="Question Section and Number" color="#FFFFFF" themeColor="#008C69" creationPolicy="auto">

       

       

       

      <mx:Script>

      <![CDATA[

       

       

      import mx.core.Application;

      ]]>

       

      </mx:Script>

       

       

       

      <mx:Text x="10" y="15" text="Application.application.survey.xQuery" fontSize="14" id="queryText" width="477" height="61" color="#3D3D3D"/>

       

       

      <mx:RadioButton x="10" y="77" label="Answer" id="queryAnswer" fontSize="14" color="#3D3D3D"/>

       

       

      <mx:HRule x="10" y="67" width="540" height="2"/>

       

       

      <mx:Button x="247" y="119" label="Next" color="#3D3D3D" themeColor="#008C69" id="querySubmit"/>

       

       

      </mx:Panel>

        • 1. Re: Accessing variable in parent of component
          oldMster Level 3

          In your component, you can access the immediate parent with parentDocument - so parentDocument.XQuery should get what you want.  However, make sure the xQuery variable is decleared as public in the parent - if it is declared as private it is not visible to an component except the one it is defined in.

           

          This works if your component will always be referencing an item in it's immediate parent.  To make it more general purpose, you would declare a public property in the component to pass the object you need to reference in with.  So, if you xQuery is a String variable, for example, your component would look like this:

           

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

          <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="580" height="202" layout="absolute" title="Question Section and Number" color="#FFFFFF" themeColor="#008C69" creationPolicy="auto">

           

          <mx:Script>

           

          <![CDATA[

          import mx.core.Application;

          [Bindable]

          public var neededData:String;

          ]]>

          </mx:Script>

           

          <mx:Text x="10" y="15" text="{neededData}" fontSize="14" id="queryText" width="477" height="61" color="#3D3D3D"/>

           

          <mx:RadioButton x="10" y="77" label="Answer" id="queryAnswer" fontSize="14" color="#3D3D3D"/>

           

          <mx:HRule x="10" y="67" width="540" height="2"/>

           

          <mx:Button x="247" y="119" label="Next" color="#3D3D3D" themeColor="#008C69" id="querySubmit"/>

           

          </mx:Panel>

           

          When calling the component in the 'parent', then you pass the neededData in:

           

          <mynamespace:myComponent id="thisComponent" neededData="value" ....../>

          or

          <mynamespace:myComponent id="thisComponent" neededData="{bindable object}" ....../>

           

          Mark

          • 2. Re: Accessing variable in parent of component
            andreamesser Level 1

            Hi,

             

            I tried your suggestion and it sounds right I think I am just implementing it incorrectly.

             

            The variable xQuery is public in the survey.mxml module code that is the parent of the component I pasted below.

             

             

             

             

             

            Revised component code:

             

             

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

             

              

             

             

            <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="580" height="202" layout="absolute" title="Question Section and Number" color="#FFFFFF" themeColor="#008C69" creationPolicy="auto" creationComplete="radioInit()">

             

            <mx:Script>

             

             

            <![CDATA[

             

            import

             

             

             

             

             

            mx.core.Application;

             

            [

            Bindable]

            public

             

             

             

             

             

            var neededData:String;

             

            public

             

             

             

             

             

            function radioInit():void

            {

            neededData = parentDocument.xQuery.question;

            }

             

            ]]>

             

            </mx:Script>

             

              

             

             

            <mx:Text x="10" y="15" fontSize="14" id="queryText" width="477" height="61" color="#3D3D3D" text="{neededData}"/>

             

            <mx:RadioButton x="10" y="77" label="Answer" id="queryAnswer" fontSize="14" color="#3D3D3D"/>

             

            <mx:HRule x="10" y="67" width="540" height="2"/>

             

            <mx:Button x="247" y="119" label="Next" color="#3D3D3D" themeColor="#008C69" id="querySubmit"/>

             

            </mx:Panel>