0

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 &amp;&amp; somethingElse == false}" etc. But I need it to do more like for loops etc.

2
  • Have you tried to do it using closures inside your binding? Commented Oct 18, 2011 at 14:15
  • 1
    I am not sure I quite understand what you are asking Commented Oct 18, 2011 at 14:24

2 Answers 2

1

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"));
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.