Flex:变更观察者是否可以观察多个变量? [英] Flex: Is it possible for a changewatcher to watch multiple variables?

查看:28
本文介绍了Flex:变更观察者是否可以观察多个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Flex 变量,它依赖于许多其他 Flex 变量.我希望这个变量在它所依赖的任何变量被更新时随时更新.是否可以使用单个 ChangeWatcher 执行此操作?

I have a Flex variable that depends on a number of other Flex variables. I want this variable to be updated any time any of the variables it depends on are updated. Is it possible to do this using a single ChangeWatcher?

具体来说,是否可以修改以下代码,使其仅使用一个 ChangeWatcher,如果可以,如何修改?

Specifically, can the following code be modified so that it uses only one ChangeWatcher and if so how?

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

    <fx:Script>
        <![CDATA[
            import mx.binding.utils.ChangeWatcher;
            import mx.events.FlexEvent;
            import mx.events.PropertyChangeEvent;

            public var watcher1:ChangeWatcher;
            public var watcher2:ChangeWatcher;
            public var watcher3:ChangeWatcher;

            public var nClicked:int = 0;
            [Bindable]
            public var buttonCounterTxt:String = "nclicked =" +  nClicked.toString();
            [Bindable]
            public var btn1Clicked:Boolean = false;
            [Bindable]
            public var btn2Clicked:Boolean = false;
            [Bindable]
            public var btn3Clicked:Boolean = false;

            protected function init(event:FlexEvent):void
            {
                watcher1 = ChangeWatcher.watch(this, 'btn1Clicked', updateNClicked);
                watcher2 = ChangeWatcher.watch(this, 'btn2Clicked', updateNClicked);
                watcher3 = ChangeWatcher.watch(this, 'btn3Clicked', updateNClicked);
            }

            protected function updateNClicked(event:Event):void{
                nClicked = 0;
                if(btn1Clicked)
                    nClicked++;
                if(btn2Clicked)
                    nClicked++;
                if(btn3Clicked)
                    nClicked++;
                buttonCounterTxt = "nclicked =" +  nClicked.toString();

            }
        ]]>
    </fx:Script>

    <s:Button id="myBtn1" x="50" y="0" click="{btn1Clicked=!btn1Clicked}" 
              label="{btn1Clicked?'unclickMe':'clickMe'}"/>
    <s:Button id="myBtn2" x="50" y="50" click="{btn2Clicked=!btn2Clicked}" 
              label="{btn2Clicked?'unclickMe':'clickMe'}"/>
    <s:Button id="myBtn3" x="50" y="100" click="{btn3Clicked=!btn3Clicked}" 
              label="{btn3Clicked?'unclickMe':'clickMe'}"/>

    <s:Label x="50" y="200" text="{buttonCounterTxt}"/>

</s:Application>

我使用的是 Flex 4.14.1.

I am using Flex 4.14.1.

推荐答案

来自 文档,这是不可能的:

From the Documentation, it is not possible:

"单个 ChangeWatcher 实例可以监视一个属性或属性链.属性链是可从宿主对象访问的属性序列.例如,表达式 obj.abc 包含属性链 (a, b, c)"

"A single ChangeWatcher instance can watch one property, or a property chain. A property chain is a sequence of properties accessible from a host object. For example, the expression obj.a.b.c contains the property chain (a, b, c)."

就我而言,这些属性不是同一属性链的一部分,因此我不能让一个观察者监视所有这些属性.

In my case the properties are not part of the same property chain, so I cannot have a single watcher watch all of them.

不过,代码可以简化.每当更新任何因变量时,都需要调用更新主变量的函数.不需要声明或命名执行此操作的 ChangeWatcher.可以删除 ChangeWatcher 声明并将 init 函数替换为以下声明:

However, the code can be simplified. The function to update the main variable needs to be called any time any of the dependent variables are updated. The ChangeWatchers that do this do not need to be declared or named. The ChangeWatcher declarations can be removed and the init function replaced with this one:

        public function init(event:FlexEvent):void{
            ChangeWatcher.watch(this, 'btn1Clicked', updateNClicked);
            ChangeWatcher.watch(this, 'btn2Clicked', updateNClicked);
            ChangeWatcher.watch(this, 'btn3Clicked', updateNClicked);
        }

这篇关于Flex:变更观察者是否可以观察多个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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