问题
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?
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>
I am using Flex 4.14.1.
回答1:
From the Documentation, it is not possible:
"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.
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);
}
来源:https://stackoverflow.com/questions/33571103/flex-is-it-possible-for-a-changewatcher-to-watch-multiple-variables