1 Reply Latest reply on Jul 18, 2006 11:16 AM by inlineblue

    Problems creating custom components

    dimival Level 1
      I have two custom components, one extends from TextInput (named CampoTexto) and the other one from FormItem (named PickList). Here are the codes:

      CampoTexto
      <mx:TextInput xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="createListener()">
      <mx:Script>
      <![CDATA[
      import com.legosoft.components.Controller;
      import com.legosoft.events.EnableControlsEvent;

      public var tag:Number;
      public var ctrl:Controller;

      private function createListener():void
      {
      ctrl.addEventListener('enableControls', enableControls);
      }

      private function enableControls(event:EnableControlsEvent):void
      {
      enabled = ((tag & event.currOp) > 0);
      }
      ]]>
      </mx:Script>
      </mx:TextInput>

      PickList
      <?xml version="1.0" encoding="utf-8"?>
      <mx:FormItem xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:ns2="myComponents.*"
      creationComplete="createListener()" direction="horizontal">
      <mx:Script>
      <![CDATA[
      import com.legosoft.components.Controller;
      import com.legosoft.events.EnableControlsEvent;

      public var tag:Number;
      public var ctrl:Controller;

      private function createListener():void
      {
      ctrl.addEventListener('enableControls', enableControls);
      }

      public function enableControls(event:EnableControlsEvent):void
      {
      enabled = ((tag & event.currOp) > 0);
      }
      ]]>
      </mx:Script>

      <ns2:CampoTexto tag="{tag}" ctrl="{ctrl}"/>
      <mx:Button label="Pick"/>

      </mx:FormItem>

      As you can see both have a Tag property and a Ctrl one, in my application.mxml i am doing the following:

      <ns2:Controller id="ctrl">
      <mx:Form>
      <ns2:PickList label="Tipo" tag="{Controller.EDIT_FIELD}" ctrl="{ctrl}"/>
      </mx:Form>

      When i run this it throws an error when trying to register de EventListener in the CampoTexto component because the ctrl property is null. On the PickList component there is no error when registering the EventListener. As you can see in the application.mxml i am passing the Controller reference (ctrl) to the PickList component and this one passes it to the CampoTexto component. The problem is that by the time the CampoTexto component is being created the tag and ctrl properties of the PickList are still null, their values have not been initialized yet, so that's why the CampoTexto component throws the error.
      Does anyone know how to solve this?? How can i initialize the properties on the PickList Component on time so when the CampoTexto components is being created it can read these values?

      Thanks
      Diego
        • 1. Re: Problems creating custom components
          inlineblue Level 1
          Add the listener when the controller is set, not on creationComplete. Use a getter/setter so you know when the controller is set:

          private var _ctrl: Controller; //Make this private

          public function set ctrl(value: Controller): void
          {
          _ctrl = value;
          if (_ctrl != null) //Sanity check
          _ctrl.addEventListener(...);
          }

          public function get ctrl(): Controller
          {
          return _ctrl;
          }