Arraycollection 没有捕获抛出的事件?
我有一个对象集合,每个对象每次其值更新时都会引发一个事件。 我试图通过向保存该事件的数组集合添加一个侦听器来捕获该事件(请参阅主类),但它不起作用。 老实说,我不确定这是正确的方法。
我避免使用 Collection.CHANGE 因为它陷入无限递归,最终导致堆栈溢出。 有任何想法吗?
[Bindable]
public class NamesVO {
public var steveList:ArrayCollection; // array of SteveVO objects
public function NamesVO() {
steveList = new ArrayCollection();
}
public function rename():void {
for each(var steve:SteveVO in steveList) {
steve.rename();
}
}
}
[Bindable]
public class SteveVO extends EventDispatcher {
public static const VALUE_CHANGED:String = "VALUE_CHANGED";
public var code:String;
public var name:String;
public var _quantity:Number;
public function SteveVO() {
this.code = "";
this.name = "";
_quantity = 0;
}
public function get quantity():Number {
return _quantity;
}
public function set quantity(quantity:Number):void {
_quantity = quantity;
dispatchEvent(new Event(VALUE_CHANGED));
}
public function rename():void {
name = code + " - " + _quantity;
}
}
Main class:
names = new NamesVO();
names.steveList.addEventListener(SteveVO.VALUE_CHANGED, function():void {
names.rename(); // this anon function is not being executed!!
});
var steve:SteveVO = new SteveVO();
names.steveList.addItem(steve);
// names is bound on a datagrid and uses itemeditor for each SteveVO object
I have a collection of objects and each object throws an event every time its value gets updated. Im trying to capture that event by adding a listener to the arraycollection that holds it (see main class) but its not working. Honestly I'm not sure this is the correct approach.
I'm avoiding using Collection.CHANGE because it fells into an infinite recursion ultimately ends in a stack overflow. Any ideas?
[Bindable]
public class NamesVO {
public var steveList:ArrayCollection; // array of SteveVO objects
public function NamesVO() {
steveList = new ArrayCollection();
}
public function rename():void {
for each(var steve:SteveVO in steveList) {
steve.rename();
}
}
}
[Bindable]
public class SteveVO extends EventDispatcher {
public static const VALUE_CHANGED:String = "VALUE_CHANGED";
public var code:String;
public var name:String;
public var _quantity:Number;
public function SteveVO() {
this.code = "";
this.name = "";
_quantity = 0;
}
public function get quantity():Number {
return _quantity;
}
public function set quantity(quantity:Number):void {
_quantity = quantity;
dispatchEvent(new Event(VALUE_CHANGED));
}
public function rename():void {
name = code + " - " + _quantity;
}
}
Main class:
names = new NamesVO();
names.steveList.addEventListener(SteveVO.VALUE_CHANGED, function():void {
names.rename(); // this anon function is not being executed!!
});
var steve:SteveVO = new SteveVO();
names.steveList.addItem(steve);
// names is bound on a datagrid and uses itemeditor for each SteveVO object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VALUE_CHANGED
事件不是由 steveList 数组 Collection 调度的,因此您的侦听器不会检测到该事件。 您可以通过检测何时将项目添加到数组集合并向调度相同事件的新 steveVO 对象添加侦听器,将所需的功能封装在NamesVO
类中来自NamesVO
。 然后只需在主类中监听该事件即可。是否有理由在更改一个数量时更改所有名称? 在 steveVO 类的 set 函数中调用 rename 会更好吗?
实现更改:
在主类中使用:
当然这只是一个示例,仅包括一次添加一项的情况。
The
VALUE_CHANGED
event is not dispatched by the steveList array Collection so won't be detected by your listener. You could encapsulate the functionality you want inside theNamesVO
class by detecting when an item is added to the array collection and adding a listener to the newsteveVO
object that dispatches the same event fromNamesVO
. Then just listen for that event in your main class.Is there a reason to change all the names when one quantity is changed. Would it be better simply to call rename inside the set function of the
steveVO
class?To implement the change:
In the main class use:
Of course this is only an example and only includes the case where one item is added at a time.