在Flex的绑定:如何自动进行变量时另一个变量发生变化更新? [英] Binding in Flex: How to make a variable automatically update when another variable is changed?

查看:175
本文介绍了在Flex的绑定:如何自动进行变量时另一个变量发生变化更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,它可以使一个部件的参数取决于变量,条件是变量包含[可绑定]标签。这意味着更新可绑定变量的值自动更新组件。

I know that it is possible to make a parameter of a component depend on a variable, provided that variable contains the [Bindable] tag. This means that updating the value of the Bindable variable automatically updates the component.

时有可能使另一个变量取决于可绑定变量?

Is it possible to make another variable depend on a Bindable variable?

在下面的例子中我有一个绑定变量btnClicked。当btnClicked由函数btnClick改变()被调用,在文本区文本按预期进行更新。

In the example below I have a variable btnClicked that is bound. When btnClicked is changed by the function btnClick() being called, the text in the TextArea is updated as expected.

我想能够使另一个变量,btnText,当btnClicked更新进行更新。我不知道如何沟通这种依赖性。在下面btnText的价值code是最初clickMe预期。然而,当btnClicked变化,按钮上的文字不更新为unclickMe。

I would like to be able to make another variable, btnText, be updated when btnClicked is updated. I do not know how to communicate this dependency. In the code below the value of btnText is initially "clickMe" as expected. However, when btnClicked changes, the text on the button does not update to "unclickMe".

<fx:Script>
    <![CDATA[
        [Bindable]
        public var btnClicked:Boolean = false;

        public function btnClick():void{
            btnClicked = !btnClicked;
        }

        [Bindable]
        public function get btnText():String{
            return btnClicked?"unclickMe":"clickMe";
        }
    ]]>
</fx:Script>

<s:Button click="btnClick()" label="{btnText}"/>

<s:TextArea y="50" text="{btnClicked?'Button is clicked':'Button is unclicked'}"/>

我怎样才能改变的上方,使得btnText时自动btnClicked改变更新code?

How can I change the code above so that btnText automatically updates when btnClicked is changed?

有没有可能有一个任意长的连接变量链每一个更新时,previous 1更新?

Would it be possible to have an arbitrarily long chain of connected variables each of which updates when the previous one updates?

推荐答案

您也可以使用<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/binding/utils/ChangeWatcher.html"相对=nofollow> 返回ChangeWatcher 以检测当你的绑定属性更新:

You can also use a ChangeWatcher to detect when your bindable property is updated :

<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" creationComplete="init(event)">

    <fx:Script>
        <![CDATA[

            import mx.binding.utils.ChangeWatcher;
            import mx.events.FlexEvent;

            public var watcher:ChangeWatcher;

            [Bindable]
            public var btnClicked:Boolean = false;
            [Bindable]
            public var btnText:String = 'clickMe';

            protected function btnClick():void {
                btnClicked = !btnClicked;
            }

            protected function init(event:FlexEvent):void
            {
                watcher = ChangeWatcher.watch(this, 'btnClicked', watcherListener);
            }

            protected function watcherListener(event:Event):void {
                btnText = btnClicked ? 'unclickMe' : 'clickMe';
            }

        ]]>
    </fx:Script>

    <s:Button x="303" y="30" label="{btnText}" click="btnClick()"/> 
    <s:TextArea x="303" id="txt" y="96" text="{btnClicked ? 'Button is clicked' : 'Button is unclicked'}"/>

</s:Application>

希望能有所帮助。

Hope that can help.

这篇关于在Flex的绑定:如何自动进行变量时另一个变量发生变化更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆