yigit escreveu:
> i think i'm misunderstood; because your solution suggestions does not
> fit my problem.
> first of all, functions can be binded. (with an event driven
> architecture that triggers
> the function to be executed and all bindable references be updated)
> to see how it is done, take a look at the article in flex api :
> http://127.0.0.1:64744/help/index.jsp?topic=/com.adobe.flexbuilder.help/html/databinding_4.html
> (or search for "[Bindable] function" then click the second result)
>
> after this quick information, i want to refigure my problem.
> assume: myInstance:MyClass; //MyClass implements a bindable toString
> method
>
> my problem has two parts:
> 1st; when i use function biding (<textInput
> text="{myInstance.toString()}"/>) , function binding works fine
> except the itemRenderer (<textInput text="{data.toString()}"/>)
> why? what is the problem? can't the VM detect the binding mechanism?
> (by the way there isn't any compile time
> warnings about the binding will not be able to run properly)
>
> 2nd: if i do not write toString (<textInput text="{myINstance}"/>),
> although it calls toString method to find the
> text value, function binding does never work (neither in itemRenderers
> nor normal usage).
> why? why cant the compiler detect the binding?
>
> thnks in advance.
>
>
> Frederico Garcia wrote On 12/26/2007 05:38 PM:
>>
>> 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:fmotagarcia%40kemelyon.com>
>> > <mailto:[EMAIL PROTECTED]
>> <mailto:fmotagarcia%40kemelyon.com>>> 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>
>> > <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
>> <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 <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 <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
>>
>
>
>
> __________ NOD32 2747 (20071225) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
Hi,
Functions are indeed bindable, but only special function are: getters
and setters. If you look carefully in the help topic you siggested the
example there is:
|* [Bindable(event="maxFontSizeChanged")]*| // Define public getter
method. |* public function get maxFontSize():Number {*| return
_maxFontSize; }
This, however is not what you want, since getters and setters work the
same way as vars, only they execute some code.
Using getters you could do something like:
[Bindable(event="changeEvent")]
public function get asString():String {
... ( some processing that results in a string var 'string1')
return string1;
}
every time you change the Object you had to trigger the "changeEvent".
Example:
public function set someProperty(value:Object):void {
dispatchEvent(new Event("changeEvent"));
}
Regarding what Kevin suggested it would work under certain circunstances.
<mx:Label id="lbl" text="{roleRef.getString(roleRef.nameID)}"/>
this would do exactly what you want (notice the 2 references to
roleRef), but you had to be careful with the parameter type. String,
int, ArrayCollection work, array don't (since arrays are not data
awhare).
Binding is one of the coolest things in flex, and one of the most
difficult to understand and use. I suggest you find a method that always
works and always use it. I've spent hours with bindings not working. For
common situations these are my solutions:
1. Binding to a var (String, int, ArrayCollection)
SOLUTION:
[Bindable] public var myVar:String = "";
<mx:Label id="lbl" text="{myVar)}"/>
NOTE: always initialize your vars (specially with arrayCollections)
2. Have a bindable var wich does something when changed:
SOLUTION:
GETTERS AND SETTERS
private var _myVar:String = "";
public function set myVar(value:String) :void{
_myVar = value;
// (...) your code here
dispatchEvent("myVarChanged");
}
[Bindable(event="myVarChanged")]
public function get myVar():String {
// (...) your code here
return _myVar;
}
<mx:Label id="lbl" text="{myVar)}"/>
3. Binding to a function
SOLUTION:
DON'T BIND TO FUNCTIONS.
3.1 use getters and setters (solution 2)
3.2 have a function changing a var and bind to that var (solution 1)