4 Replies Latest reply on Jul 11, 2006 2:59 PM by ddanone

    getChildByName inside a Class to get an Application object

    ddanone Level 1
      I have a class in a separate .as inside a package.

      I want to get the object of one component (mx:TextInput) from that class to get some properties.

      How can it make?

      Thanks in advance
        • 1. Re: getChildByName inside a Class to get an Application object
          peterent Level 2
          You do not say what, if any, class your class extends. But if your class extends UIComponent or any of its subclasses, then you can use the parentApplication property to reference that component.

          But a lot depends on where your class is instantiated within the component layout.
          • 2. Re: getChildByName inside a Class to get an Application object
            Flex harUI Adobe Employee
            can you post some code. I'm not clear on what you're asking
            • 3. getChildByName inside a Class to get an Application object
              ddanone Level 1
              On main.mxml:
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:ctls="ctls.*">
              <mx:TabNavigator width="100%" height="300" id="TabNav">
              <mx:Canvas label="Datos Personales">
              <mx:Grid id='gridDP'>
              <mx:GridRow>
              ...
              <mx:GridItem> <mx:TextInput id=" ape" maxChars='30' width='200'/> </mx:GridItem>
              ...
              <ctls:CheckOnServer required="false" url=' http://localhost/flex/chkcp.php' params='var0=ape' />
              ...

              On CheckOnServer.as:

              package ctls
              {
              import mx.controls.TextInput;
              import mx.rpc.http.HTTPService;
              import flash.events.Event;
              import mx.utils.StringUtil;
              import mx.controls.Alert;
              import flash.display.DisplayObject;

              public class CheckOnServer extends TextInput {

              public var required:Boolean;
              public var url:String;
              public var params:String;

              private var hs:HTTPService=new HTTPService(null);
              private var results:Array;
              private var _p:Array=new Array();
              private var o:DisplayObject;

              // Constructor
              //----------------------------------------------
              public function CheckOnServer() {
              super();
              addEventListener("creationComplete", myCC);
              }
              //----------------------------------------------
              private function myCC(eventObj:Event):void {
              hs.resultFormat='text';
              hs.url=this.url;
              hs.addEventListener("result",getResult);

              var t:Array;
              var r:Array;

              t=this.params.split('|');

              for each (var item:String in t) {
              r=item.split('=');
              this._p[ r[0] ]=r[1]; // r[0]='var0' & r[1]='ape' -> this.p['var0']='ape';
              }
              addEventListener("focusOut", chkChg);
              }
              //----------------------------------------------
              private function chkChg(event:Event):void{
              for each (var n:String in _p) {
              // NOW I WANT TO GET THE OBJECT BUT IT RETURNS 'null'
              this.o=parentApplication.rawChildren.getChildByName( n );
              Alert.show( String(this.o) );
              }
              }
              private function getResult(event:Event):void{
              }
              }
              }
              • 4. getChildByName inside a Class to get an Application object
                ddanone Level 1
                Solved the problem in other way:

                The purpose of CheckOnServer component is to validate the data entered against a php server and depending of the php response modify the fields indicated on "campos" array or give an error. The class is not yet finished.

                Thanks anyway folks!!



                In main.mxml:
                <?xml version="1.0" encoding="iso-8859-1"?>
                <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:ctls="ctls.*" applicationComplete="init()">
                ...
                [Bindable]
                public var campos:Array=new Array();
                public function init():void{
                var o:Array=new Array();
                o['ape']=new Object();
                o['ape']=ape;
                campos=o; //trigger the binding data
                }
                ...
                <mx:TextInput id="ape" maxChars='30' width='200'/>
                ...
                <ctls:CheckOnServer required="false" url=' http://localhost/flex/chkcp.php' campos='{campos}'/>
                ...


                At CheckOnServer.as:
                package ctls{
                import mx.controls.TextInput;
                import mx.rpc.http.HTTPService;
                import flash.events.Event;
                import mx.utils.StringUtil;
                import mx.controls.Alert;

                public class CheckOnServer extends TextInput {

                public var required:Boolean;
                public var url:String;
                public var campos:Array=new Array();

                private var hs:HTTPService=new HTTPService(null);
                private var results:Array;
                private var lastValue:String;

                // Constructor
                //----------------------------------------------
                public function CheckOnServer() {
                super();
                addEventListener("creationComplete", myCC);
                }
                //----------------------------------------------------------------------------------------
                private function myCC(eventObj:Event):void {

                hs.resultFormat='text';
                hs.url=this.url;
                hs.addEventListener("result",getResult);

                addEventListener("focusOut", chkChg);
                }
                //--------------------------------------------------------------------------------------- -
                private function chkChg(event:Event):void{

                if(StringUtil.trim(this.text)=='') return;
                if(this.text==this.lastValue) return;
                this.lastValue=this.text;

                var a:Array=new Array();
                var s:String;
                for (var prop:String in campos) {
                a[a.length] = prop+'='+campos[prop].text;
                }
                s=a.join('|');

                hs.url=this.url+'?'+s;
                hs.send();
                }

                private function getResult(event:Event):void{
                var s:String=String(this.hs.lastResult);
                var a:Array=s.split('|');
                if( a[0]=='OK' ){
                for(var x:Number=1; x<a.length; x+=2){
                for (var prop:String in campos) {
                if(prop==a[x]){
                campos[prop].text=a[x+1];
                }
                }
                // Alert.show( a[x]+'--'+a[x+1] );
                }
                }else{
                Alert.show('Error: '+a[1]);
                }
                // Alert.show( String(this.hs.lastResult) );
                }

                }
                }


                At chkcp.php:
                <?
                echo 'OK|ape|hola '.$ape;
                exit;
                ?>