Arraycollection 没有捕获抛出的事件?

发布于 2024-07-30 01:42:28 字数 1587 浏览 4 评论 0原文

我有一个对象集合,每个对象每次其值更新时都会引发一个事件。 我试图通过向保存该事件的数组集合添加一个侦听器来捕获该事件(请参阅主类),但它不起作用。 老实说,我不确定这是正确的方法。

我避免使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑕疵 2024-08-06 01:42:28

VALUE_CHANGED 事件不是由 steveList 数组 Collection 调度的,因此您的侦听器不会检测到该事件。 您可以通过检测何时将项目添加到数组集合并向调度相同事件的新 steveVO 对象添加侦听器,将所需的功能封装在 NamesVO 类中来自NamesVO。 然后只需在主类中监听该事件即可。

是否有理由在更改一个数量时更改所有名称? 在 steveVO 类的 set 函数中调用 rename 会更好吗?

实现更改:

import flash.events.Event;
import mx.collections.ArrayCollection;
import mx.events.CollectionEvent;
import mx.events.CollectionEventKind;

[Bindable]
public class namesVO 
{                  
    public var steveList:ArrayCollection;   // array of SteveVO objects       
    public function namesVO() 
    {                
        steveList = new ArrayCollection();
        steveList.addEventListener(CollectionEvent.COLLECTION_CHANGE,collChanged);   
    }

    private function collChanged(e:CollectionEvent):void
    {
        if (e.kind == CollectionEventKind.ADD)
        e.items[0].addEventListener(steveVO.VALUE_CHANGED,valueChanged);
    }                      

    private function valueChanged(e:Event):void
    {
        dispatchEvent(new Event(steveVO.VALUE_CHANGED));
    }

    public function rename():void 
    {                
        for each(var steve:steveVO in steveList) 
        {                              
            steve.rename();                
        }  
    }                             
}

在主类中使用:

names = new namesVO();
names.addEventListener(steveVO.VALUE_CHANGED, function():void 
{     
    names.rename();  
}); 

steve = new steveVO();
names.steveList.addItem(steve); 
steve.quantity = 12;

当然这只是一个示例,仅包括一次添加一项的情况。

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 the NamesVO class by detecting when an item is added to the array collection and adding a listener to the new steveVO object that dispatches the same event from NamesVO. 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:

import flash.events.Event;
import mx.collections.ArrayCollection;
import mx.events.CollectionEvent;
import mx.events.CollectionEventKind;

[Bindable]
public class namesVO 
{                  
    public var steveList:ArrayCollection;   // array of SteveVO objects       
    public function namesVO() 
    {                
        steveList = new ArrayCollection();
        steveList.addEventListener(CollectionEvent.COLLECTION_CHANGE,collChanged);   
    }

    private function collChanged(e:CollectionEvent):void
    {
        if (e.kind == CollectionEventKind.ADD)
        e.items[0].addEventListener(steveVO.VALUE_CHANGED,valueChanged);
    }                      

    private function valueChanged(e:Event):void
    {
        dispatchEvent(new Event(steveVO.VALUE_CHANGED));
    }

    public function rename():void 
    {                
        for each(var steve:steveVO in steveList) 
        {                              
            steve.rename();                
        }  
    }                             
}

In the main class use:

names = new namesVO();
names.addEventListener(steveVO.VALUE_CHANGED, function():void 
{     
    names.rename();  
}); 

steve = new steveVO();
names.steveList.addItem(steve); 
steve.quantity = 12;

Of course this is only an example and only includes the case where one item is added at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文