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.