Binding to getters or read-only functions is tricky. I haven't found a really elegant OOP approach, although others have offered solutions like dispatching event from functions etc..

http://www.rubenswieringa.com/blog/binding-read-only-accessors-in-flex

The basic problem is that the binding has no idea when to update since it has no knowledge of what properties are inside the function.

When I bind to functions, if I want to ensure that they always update, I pass in methods parameters used to trigger binding as explain in the Adobe docs. This would make your code look like this:

<mx:Label id="lbl" text="{getString(roleRef.nameID)}"/>

The getString function would call toString on nameID

See using ActionScript functions in curly braces:
http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/ wwhelp.htm?context=LiveDocs_Book_Parts&file=databinding_091_03.html

I would be interested if you find another solution.

- Kevin



On Dec 26, 2007, at 10:38 AM, Frederico Garcia wrote:

Jhonny Everson escreveu:
>
> I guess that your solution has a problem, the circular reference to
> toString.
>
> it could be something like:
>
> [Bindable] public var stringValue:String = "";
> public function toString():String {
>
> ... ( some processing that results in a string var 'string1')
>
> stringValue= string1;
> return stringValue;
> }
>
>
> On 12/26/07, * Frederico Garcia* <[EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>> wrote:
>
> yigit escreveu:
> > hi all;
> > i have a custom class which has a toString method; so i can
> directly use
> > it as a source to textInput's text field.
> > i want to make binding work, i mean when the result of the toString
> > changes, i want the view to update itself automatically.
> > nameID field of roleRef is an instance of my class that implements
> > toString with function biding.
> > when i use this way:
> > <mx:Label id="lbl" text="{roleRef.nameID}"/>
> > function binding on toString does not work
> > when i use this way:
> > <mx:Label id="lbl" text="{roleRef.nameID.toString()}"/>
> > function biding on toString works, but inside an item renderer,
> it does
> > not work.
> > <mx:Label id="lbl" text="{data.nameID.toString()}"/>
> >
> >
> > is this a compiler bug or is this the normal behavior?
> >
> >
> > --
> > Flexcoders Mailing List
> > FAQ:
> http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> <http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt>
> > Search Archives:
> http://www.mail-archive.com/flexcoders%40yahoogroups.com
> > Yahoo! Groups Links
> >
> >
> >
> >
> > __________ NOD32 2747 (20071225) Information __________
> >
> > This message was checked by NOD32 antivirus system.
> > http://www.eset.com
> >
> >
> >
> >
> I believe you can only bind vars and setters. By binding a function i
> think it will only execute the function once. An easy workaround
> is to
> have a var containg the result of the toString function and bind the
> property to that var. Something like:
>
> [Bindable] public var stringValue:String = "";
> public function toString():String {
> stringValue= this.toString();
> return stringValue;
> }
>
> <mx:Label id="lbl" text="{data.nameID.stringValue}"/>
>
> Regards
>
> Frederico Garcia
>
>
>
>
> --
> Jhonny Everson
>
> __________ NOD32 2747 (20071225) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
Yes, indeed there was a circular reference to toString. Thanks for the
correction. The general concept is the same though, and I think it's the
best way to solve the "bind to function" problem.

Regards

Frederico Garcia



Reply via email to