观察可绑定属性
在我的 Flex 应用程序中,我有一个公共可绑定属性。 我希望每次该属性的值发生变化时都会触发一个函数。 我尝试使用 ChangeWatchers,但似乎这些仅适用于内置组件,例如文本框更改。 我想对运行时更改的属性执行相同的行为。
In my flex app I have a public bindable property.
I want it so that every time the value of that property changes, a function gets triggered.
I tried using ChangeWatchers, but it seems those only apply to built-in components like a text box change.
I would like to do that same behavior with a property that changes at runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种选择是使用 BindingUtils.bindSetter(它会顺便返回 ChangeWatcher):
在这里,只要
myValue
属性发生更改,就会调用myValueChanged
。当然还有其他方法,但我经常使用这种方法,效果很好。希望有帮助!带着问题回来发帖,我会留意的。One option is to use
BindingUtils.bindSetter
(which incidentally returns a ChangeWatcher):Here,
myValueChanged
gets called whenever themyValue
property changes. There are other ways, of course, but I often use this approach with good results. Hope it helps! Post back with questions and I'll keep an eye out.按照 back2dos 的建议查看 BindUtils 类。
而且,您还可以设置在对属性进行更改时将触发的事件的名称(默认为 propertyChange),如下所示:
也就是说,如果 ChangeWatchers 添加更改事件而不是 propertyChange 事件的侦听器。这有点奇怪,但对于 Flex SDK 的所有不幸来说并非不可能。
但同样,我认为 BindUtils 类应该可以满足您的需要。
Look into BindUtils class as back2dos suggests.
And, also, you can set the name of the event that will be triggered when a change is done to a property (default is propertyChange) like this:
That is if ChangeWatchers adds listeners for the change event instead of propertyChange event. Which would be kind of weird, but not impossible with all the mishaps of the flex SDKs.
But again, I think BindUtils class should do the trick for you.
使用 ObjectProxy 类或其子类并包装具有您需要监视的属性的类。在我的示例中,如果有人更改财产工资,在对象 Person 中将其值设置为超过 55000,我将调用一个 func:
package com.farata
{
导入 mx.utils.ObjectProxy;
导入 flash.utils.*;
使用命名空间 flash_proxy;
if (姓名=='工资'&值>55000) {
// 向该实例添加一个新属性
// Person类,可用于计算
// 总报酬
setProperty("养老金", 0.02);
}
super.setProperty(名称, 值);
}
}
}
Use the class ObjectProxy or its subclass and wrap up the class that has a property you need to watch. In my example, I'm calling a func if someone is changing the property salary giving it a value of more than 55000 in an object Person:
package com.farata
{
import mx.utils.ObjectProxy;
import flash.utils.*;
use namespace flash_proxy;
if ( name == 'salary'&& value>55000) {
// add a new property to this instance of the
// class Person, which can be used in the calculations
// of the total compensation
setProperty("pension", 0.02);
}
super.setProperty(name, value);
}
}
}
好吧,最简单的方法是监听
PropertyChangeEvent.PROPERTY_CHANGE
...如果您声明一个属性可绑定,那么 mxmlc 会生成代码来调度此事件...如果您让编译器保留生成的 ActionScript ,然后你就会看到它......除此之外,你可能想看看
BindingUtils
...well, the easiest way is to listen to
PropertyChangeEvent.PROPERTY_CHANGE
... if you declare a property bindable, then mxmlc generates the code to dispatch this event ... if you let the compiler keep the generated ActionScript, then you'll see it ...other than that, you might want to have a look at
BindingUtils
...