2 Replies Latest reply on Nov 7, 2006 2:33 PM by dimival

    External custom component referencing a component within main.mxml

    NUMB-LOCK
      Hi All,

      I have a DataGrid in its own custom component in a file named EmailDG.mxml. I also have an event where whenever a row in the DataGrid is selected, that row's information is set in another component (form, textarea, label, etc) found in my main window which is in the file, "main.mxml."

      // Found in EmailDG
      private function rowSelected(event:Event):void {
      componentToUpdate.text = event.target.selectedItem.xml_element_name;
      }

      How do I reference "componentToUpdate" found in main.mxml from EmailDG?

      Thanks,

      Bob
        • 1. Re: External custom component referencing a component within main.mxml
          SkiShakakai
          The real answer is that you don't.
          In EmailDG, create a public function that returns the selected row e.g.
          public function funcName():RETURNTYPE{
          return event.target.selectedItem.xml_element_name;
          }.
          The event you are dispatching should be coming from the EmailDG, like this:
          <mx:Metadata>
          [Event(name="EmailDGEvent", type="flash.events.Event")]
          </mx:Metadata>
          Dispatch it by attached an Click handler or change handler to the Datagrid, change="dispatchEvent(new Event('EmailDGEvent'))"

          Then in the main.mxml file, on the comp:EmailDG (where comp is the namespace for the custom component) enter a function to handle the event like this
          function myHandler(e:Event){
          //some stuff here
          }
          <comp:EmailDG EmailDGEvent="myHandler(event);"/>

          This will encapsulate the component and create a nice loose coupling that everyone talks about.
          Hopefully this help.
          Cheers,
          Todd
          email:culltp1 at gmail.com
          • 2. Re: External custom component referencing a component within main.mxml
            dimival Level 1
            Yes you can reference the other component in your application. Use Application.application.componentToUpdate

            import mx.core.Application;
            private function rowSelected(event:Event):void {
            Application.application.componentToUpdate.text = event.target.selectedItem.xml_element_name;
            }