Thanks Alex... everything works fine... :D

I will post here the code if anyone want use it:

=====================================================================
Code of component:
=====================================================================

<?xml version="1.0" encoding="utf-8"?>
<mx:Label text="{String(_charNumber)}"
xmlns:mx="http://www.adobe.com/2006/mxml"; initialize="init()" >
        
        <mx:Script>
                <![CDATA[       
                        
                        [Bindable]
                        private var _charNumber:Number;
                        [Bindable]
                        private var _maxChars:Number;
                        import mx.controls.Alert;
                        
                        private function init():void{
                                _charNumber = _maxChars;                        
        
                        }                       
                
                        public function set maxChars(value:Number):void{
                                _maxChars = value;
                                init();
                        }
                        
                        public function get maxChars():Number{
                                return _maxChars;
                        }                                               
                        
                        public function countChars(value:Number):void{
                                var maxChars :Number = _maxChars;               
                                                                                
                                _charNumber = maxChars - value;
                        }                                               
                ]]>
        </mx:Script>
</mx:Label>

=====================================================================
How to use:
=====================================================================

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:com="*"
xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute">
        <mx:TextInput id="tiTitulo" width="200" maxChars="100"
change="descCounter.countChars(tiTitulo.length)"/>
        <com:CharCounter id="descCounter" maxChars="{tiTitulo.maxChars}" />
</mx:Application>


--- In flexcoders@yahoogroups.com, "Alex Harui" <[EMAIL PROTECTED]> wrote:
>
> You can't control the order in which bindings are evaluated.  In this
> case the label's text binding fires before the maxChar binding fires,
> but it could be the other way in some other configuration.
> 
>  
> 
> It appears that the user's intention is to set the label's text to a
> number that is a function of both maxChars and length of the TextInput.
> The only "correct" way is to have a data model where the data in the
> TextInput is in the model and the TI and the Label share that string and
> can bind to its values.  Otherwise, if the TextInput had some initial
> text in it, there would be no way to determine the correct value for the
> Label.
> 
>  
> 
> If you assume the TI is empty, you can get this example working by
> calling init() in the maxChars setter:
> 
>  
> 
> public function set maxChars(value:Number):void{
> 
> _maxChars = value;
> 
> init();
> 
> trace("MAXCHARS"+_maxChars);
> 
> }
> 
>  
> 
> ________________________________
> 
> From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
> Behalf Of Tracy Spratt
> Sent: Friday, February 15, 2008 11:44 AM
> To: flexcoders@yahoogroups.com
> Subject: RE: [flexcoders] Re: Adobe SDK, why flex comps accept bind in
> properties and custom comps dont?
> 
>  
> 
> I am trying to think of a way to make the binding fire after the
> maxChars is set on the first component.  Calling an invalidate method
> will make the component re-render.  Just thinking aloud here. 
> 
>  
> 
> Maybe you can debug this by stepping through the initializing of the
> components?
> 
>  
> 
> There will be other ways to do what you want, depending on the details
> of what that is.  For instance, pass a reference to the controlling
> component into the custom component, and then read and set maxChars in
> creationComplete.
> 
>  
> 
> Tracy
> 
>  
> 
> ________________________________
> 
> From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
> Behalf Of danielvlopes
> Sent: Friday, February 15, 2008 12:53 PM
> To: flexcoders@yahoogroups.com
> Subject: [flexcoders] Re: Adobe SDK, why flex comps accept bind in
> properties and custom comps dont?
> 
>  
> 
> Sorry Tracy, i don't know what talking about with "Perhaps an
> invalidate of some kind?".... can you explain more?
> 
> Thanks.
> 
> --- In flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>
> , "Tracy Spratt" <tspratt@> wrote:
> >
> > The OP is using a public setter function to implement his public
> > property, instead of a public variable, which is fine, even
> prefereable.
> > 
> > 
> > 
> > And the trace shows that his setter is being called, but with the
> > incorrect value.
> > 
> > 
> > 
> > I suspect a timing issue, but can't think of a solution. Perhaps an
> > invalidate of some kind?
> > 
> > 
> > 
> > Tracy
> > 
> > 
> > 
> > ________________________________
> > 
> > From: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>
> [mailto:flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>
> ] On
> > Behalf Of valdhor
> > Sent: Thursday, February 14, 2008 11:12 AM
> > To: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com> 
> > Subject: [flexcoders] Re: Adobe SDK, why flex comps accept bind in
> > properties and custom comps dont?
> > 
> > 
> > 
> > AFAIK (I am a newb to flex) to be able to set a property in a custom
> > component, that property must be public. When Flex compiles the
> > application, mxml components are compiled into actionscript classes so
> > to modify the property it must be available to outside classes. Making
> > it private denies other objects from doing so.
> > 
> > Also, you are running your init method on initialize. I think you
> > meant for it to run on creationcomplete (This is why you are getting
> > NaN).
> > 
> > I have modified your code to reflect the above:
> > 
> > Application:
> > <?xml version="1.0" encoding="utf-8"?>
> > <mx:Application xmlns:com="*"
> > xmlns:mx="http://www.adobe.com/2006/mxml
> <http://www.adobe.com/2006/mxml> 
> > <http://www.adobe.com/2006/mxml <http://www.adobe.com/2006/mxml> > "
> layout="absolute">
> > <mx:TextInput text="100" id="tiTitulo" width="200" maxChars="100"
> > change="descCounter.countChars(tiTitulo.length)"/>
> > <com:CharCounter y="30" id="descCounter"
> > _maxChars="{tiTitulo.maxChars}" />
> > </mx:Application>
> > 
> > Custom Component:
> > <?xml version="1.0" encoding="utf-8"?>
> > <mx:Label xmlns:mx="http://www.adobe.com/2006/mxml
> <http://www.adobe.com/2006/mxml> 
> > <http://www.adobe.com/2006/mxml <http://www.adobe.com/2006/mxml> > "
> > creationComplete="init()" >
> > 
> > <mx:Script>
> > <![CDATA[
> > 
> > [Bindable] private var _charNumber:Number;
> > [Bindable] public var _maxChars:Number;
> > 
> > private function init():void
> > {
> > _charNumber = _maxChars;
> > this.text = this._charNumber.toString();
> > }
> > 
> > public function set maxChars(value:Number):void
> > {
> > _maxChars = value;
> > trace("MAXCHARS" + _maxChars);
> > }
> > 
> > public function get maxChars():Number
> > {
> > return _maxChars;
> > }
> > 
> > public function countChars(value:Number):void
> > {
> > var maxChars:Number = _maxChars;
> > _charNumber = maxChars - value;
> > this.text = _charNumber.toString();
> > }
> > ]]>
> > </mx:Script>
> > </mx:Label>
> >
>


Reply via email to