Reputation: 936
Can I write a function inside an attribute in flex? Something like this:
<s:Button id="btn" label="text" visible="{foo()}"/>
private function foo():Boolean
{
//do something
}
It seems it doesn't work at least for me.
I know that I can write like visible="{something == true && somethingElse == false}"
etc. But I need it to do more like for
loops etc.
Upvotes: 0
Views: 685
Reputation: 336
Try this:
[Bindable(event="update")]
private function foo():Boolean
{
return a && b && c;
}
and when a or b or c is change just do this:
dispatchEven(new Event("update"));
Upvotes: 1
Reputation: 15955
This implementation is not bindable - if the result of foo() changes, it will not be reflected on your display list.
Although, I think this should work once upon creation complete:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955"
minHeight="600">
<fx:Script>
<![CDATA[
private function foo():Boolean
{
return false;
}
]]>
</fx:Script>
<s:Button label="text"
visible="{foo()}" />
</s:Application>
A better approach would be to incorporate a presentation model, leveraging binding as seen here:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955"
minHeight="600"
creationComplete="creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
[Bindable]
public var presentationVisible:Boolean = true;
private var timer:Timer = new Timer(500);
private function foo():void
{
presentationVisible = Math.random() > 0.5 ? true : false;
}
protected function creationCompleteHandler(event:FlexEvent):void
{
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
protected function timerHandler(event:TimerEvent):void
{
foo();
}
]]>
</fx:Script>
<s:Button label="text"
visible="{presentationVisible}" />
</s:Application>
Upvotes: 0