2 Replies Latest reply on Jan 4, 2010 9:20 AM by Jerry62712

    How do I get components to work?

    Jerry62712 Level 1

      I have a 2K mxml file that I want to break into smaller pieces.  I thought making each of the different states/forms a component would do the job.  First I took all the scripts and broke them the same so that I could include the script with the component.  Was that the wrong approach to take?

       

      When I tried to compile it, the scripts that referred to stuff in the main mxml and stuff in the component would get errors.  In the main mxml I have the XML record received from an SQL database.  In the component I have fields that are populated from that record and eventually may populate that record.

       

      Main mxml has:

      <mx:XML id="dmResults">
          <data>
              <RecProcess>
                  <ADDRESS />
                  <AGE />
                  <AGENCYCONTACTNUMBER />
                  <AGENCYNAME />
                  <BTSSTAFF />
                  <CHILDRENSERVICES />
                  <CILA />
                  <CITY />
                  <CLF />
                  <CONTACT />
                  <CONTACTNAME />
                  <COUNTY />
                  <DATEDECEASED />
      ...

      Script included in the component has:

      private function gPopulateForm():void
      {
          gFixInputData();

          ADDRESS.text = XMLRecord.ADDRESS;
          CITY.text = XMLRecord.CITY;
          GUARDIANOF.text = XMLRecord.FIRSTNAME + " " + XMLRecord.LASTNAME;
          GUARDIANFNAME.text = XMLRecord.GUARDIANFNAME;
          GUARDIANLNAME.text = XMLRecord.GUARDIANLNAME;

      Guardian.mxml (component) has:

          <mx:FormItem id="gGuardianLast"
              label="Guardian Last Name:"
              indicatorGap="5"
              labelStyleName="labelRight"
              labelWidth="185" width="50%">
              <mx:TextInput id="GUARDIANLNAME"
                   maxChars="50"
                   tabIndex="12"/>
              </mx:FormItem>

            <mx:FormItem id="gGuardianFirst"
                   label="Guardian First Name:"
                   indicatorGap="5"
                   labelStyleName="labelRight"
                   labelWidth="185" width="50%">
                   <mx:TextInput id="GUARDIANFNAME"
                        maxChars="50"
                        tabIndex="13"/>
             </mx:FormItem>

      [The goofy labelWidth and CSS class are to right align the field labels.]

       

      Should I:

      1) keep the scripting in the main mxml (how to reference it from the mxml)

      or

      2) keep the scripting in the component (how to reference data in main mxml)

       

      Thanks In Advance

        • 1. Re: How do I get components to work?
          pauland Level 4

          Jerry62712 wrote:

           

          I have a 2K mxml file that I want to break into smaller pieces.  I thought making each of the different states/forms a component would do the job.  First I took all the scripts and broke them the same so that I could include the script with the component.  Was that the wrong approach to take?

           

          When I tried to compile it, the scripts that referred to stuff in the main mxml and stuff in the component would get errors.  In the main mxml I have the XML record received from an SQL database.  In the component I have fields that are populated from that record and eventually may populate that record.

           

          SNIP

           

          Should I:

          1) keep the scripting in the main mxml (how to reference it from the mxml)

          or

          2) keep the scripting in the component (how to reference data in main mxml)

           

          Thanks In Advance

           

          MXML files compile to classes and one MXML file doesn't have knowledge of the contents of the others just as a class only has knowledge of it'self or its super class.

           

          It's generally bad practice to build components that are dependent on knowing what is inside the other components.

           

          There are a few techniques at your disposal:

           

          Parameters (passing refernces to the parent class data)

          Public variables (not really recommended)

          Events

          Singleton classes.

           

          I suspect that your programming skills may not be up to using all those techniques yet.

           

          Paul

          • 2. Re: How do I get components to work?
            Jerry62712 Level 1

            I suspect you are right.  And your wording was very kind.  Here is what I've done so far:

             

            • keep the scripts that reference both component properties and main data structures in the main mxml and prefex the component properties with the id of the component
            • have the buttons (save, add, reset) dispatch events that are known to the main mxml via the instantiation of the component which calls the functions (there may be a better way to do this)
            • separate component only functions (accessibility for now) and instantiate it in the component

             

            I'm syntactly correct (no little red doohickies) if logically deficient.  When I try to add the data in the component to the record I get an HTTP error, but I was getting that before and will have to research it.

             

            BTW, I found the method was not in "Adobe Flex 3 Bible" (978 pages no CD $44.99) in which the index had no references to working with components.  I did find it in "The Essential Guide to Flex 3" (579 pages w/CD $54.99) which did have a nice reference.  Those are retail prices - web prices was a lot less for "Essential" and I got the other one for Christmas retail.