问题描述
我有一个变量,例如nClicked,这取决于许多其它变量,例如,对btn1Clicked,btn2Clicked。我想nClicked进行更新的任何时间任何变量是取决于被更新。有一种方法使用返回ChangeWatcher来更新变量,看到@akmozo,here.是否有可能使一个单一的手表返回ChangeWatcher多个变量?
具体而言,可以从以下code进行修改,以便它使用只有一个返回ChangeWatcher如果又如何?
< XML版本=1.0编码=UTF-8&GT?;
< S:应用的xmlns:FX =http://ns.adobe.com/mxml/2009
XMLNS:S =库://ns.adobe.com/flex/spark
的xmlns:MX =库://ns.adobe.com/flex/mx=了minWidth955了minHeight =600
的creationComplete =的init(事件);>
< FX:脚本>
<![CDATA [
进口mx.binding.utils.ChangeWatcher;
进口mx.events.FlexEvent;
进口mx.events.PropertyChangeEvent;
公共变种watcher1:返回ChangeWatcher;
公共变种watcher2:返回ChangeWatcher;
公共变种watcher3:返回ChangeWatcher;
公共变种nClicked:= 0;
[可绑定]
公共变种buttonCounterTxt:字符串=nclicked =+ nClicked.toString();
[可绑定]
公共变种btn1Clicked:布尔= FALSE;
[可绑定]
公共变种btn2Clicked:布尔= FALSE;
[可绑定]
公共变种btn3Clicked:布尔= FALSE;
保护功能的init(事件:FlexEvent):无效
{
watcher1 = ChangeWatcher.watch(这一点,btn1Clicked,updateNClicked);
watcher2 = ChangeWatcher.watch(这一点,btn2Clicked,updateNClicked);
watcher3 = ChangeWatcher.watch(这一点,btn3Clicked,updateNClicked);
}
保护功能updateNClicked(事件:事件):无效{
nClicked = 0;
如果(btn1Clicked)
nClicked ++;
如果(btn2Clicked)
nClicked ++;
如果(btn3Clicked)
nClicked ++;
buttonCounterTxt =nclicked =+ nClicked.toString();
}
]]≥
< / FX:脚本>
< S:按钮的ID =myBtn1X =50Y =0点击={!btn1Clicked = btn1Clicked}
标签={?btn1ClickedunclickMe':'clickMe'}/>
< S:按钮的ID =myBtn2X =50Y =50点击={!btn2Clicked = btn2Clicked}
标签={?btn2ClickedunclickMe':'clickMe'}/>
< S:按钮的ID =myBtn3X =50Y =100点击={!btn3Clicked = btn3Clicked}
标签={?btn3ClickedunclickMe':'clickMe'}/>
< S:标号x =50Y =200文本={buttonCounterTxt}/>
< / S:用途>
我使用Flex 4.14.1。
我要澄清,这是一个简单的例子。在我的真正的问题,也有很多的变化,称他们为X1,X2 ... XN,整个一个大项目。还有一个变量,我们称之为Y,取决于所有这些变量。如果有任何X1的... Xn被更新,Y必须得更新。我想为code,做了更新,以位于绕Y;即对X的不要做任何的工作。这就是为什么我使用的是返回ChangeWatcher中,x可以无需添加任何code在x一边看着。
从文档时,它是不可能的:
一个单一的ChangeWatcher实例可以看一个属性或属性链,一个产业链是从宿主对象访问属性的顺序,例如,前pression obj.abc包含属性链(一,B,C)。
在我的案件的性质是不一样的产业链的一部分,所以我不能有一个观察者看着他们。
然而,code可以被简化。需要更新主变量的函数被调用的任何时间任何因变量被更新。即做到这一点的ChangeWatchers不需要声明或命名。该声明返回ChangeWatcher可以被删除,这一个初始化函数改为:
公共职能的init(事件:FlexEvent):无效{
ChangeWatcher.watch(这一点,btn1Clicked,updateNClicked);
ChangeWatcher.watch(这一点,btn2Clicked,updateNClicked);
ChangeWatcher.watch(这一点,btn3Clicked,updateNClicked);
}
I have a variable, e.g. nClicked, that depends on a number of other variables, e.g. btn1Clicked, btn2Clicked. I want nClicked to be updated any time any of the variables is depends on are updated. There is a way to update variables using a ChangeWatcher, see the answer by @akmozo, here. Is it possible to make a single ChangeWatcher watch multiple variables?
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.
I should clarify that this is a simplified example. In my real problem, there are numerous variables, call them x1, x2 ... xn, throughout a large project. There is another variable, let's call it y, that depends on all of these variables. If any of x1 ... xn are updated, y needs to be updated too. I would like for the code that does the updating to be located around y; i.e. for the x's not to have to do any of the work. This is why I am using a ChangeWatcher, the x's can be watched without needing to add any code on the x's side.
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);
}
这篇关于软硬度:是否有可能在一个返回ChangeWatcher观看多个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!