-1

`我的“模型”是一个扩展 EventDispatcher 的 AS 类:

MeetingInfoModel extends EventDispatcher

在这个类中,我广播了一个自定义事件:

var eventObj:CustomEvent = new CustomEvent( CustomEvent.UPDATE_DOC_COUNTER );
        dispatchEvent( eventObj );

我在类的顶部包含一个元数据标签:

[Event(name="updateDocCounter", type="com.fmr.transporter.events.CustomEvent")]

我尝试在 MXML 组件中侦听此事件:

this.addEventListener( CustomEvent.UPDATE_DOC_COUNTER, onDocUpdate );

但它永远不会到达这个听众。

我遇到过很多这样的问题,我认为事件过程中有一个我不理解的关键部分。

任何人都可以提供任何有用的线索吗?

谢谢!

更新:

回应以下所有评论(感谢您的所有回复!):

  1. MeetingInfoModel 不是显示组件,不应该负责广播事件;那是我没有得到的部分!

这是我的代码: 在 MeetingInfoModel 构造函数中,我监听其类成员之一的集合更改事件:

docsAndAttachmentsList.addEventListener( CollectionEvent.COLLECTION_CHANGE, updateDocsCounter );

在该处理程序中,我尝试广播一个 MXML 组件(即显示层次结构的一部分)将处理的事件:

private function updateDocsCounter( event:CollectionEvent ):void
    {           
        var eventObj:CustomEvent = new CustomEvent( CustomEvent.UPDATE_DOC_COUNTER );
        dispatchEvent( eventObj );
    }

回到 MXML 组件,我从 creationComplete 处理程序调用此方法:

private function addListeners():void{
            MeetingInfoModel.getInstance().addEventListener( CustomEvent.UPDATE_DOC_COUNTER, onDocUpdate );
        }

听起来我应该只听 MXML 组件上的集合更改事件。我试过了,但它不起作用:

MeetingInfo.getInstance().docsAndAttachmentsList.addEventListener( CollectionEvent.COLLECTION_CHANGE, updateDocsCounter );

我不知道为什么这不起作用;这似乎是最好的解决方案。

这是完整的 MeetingInfoModel 类:

[Bindable]
[Event(name="updateDocCounter", type="com.fmr.transporter.events.CustomEvent")]
public final class MeetingInfoModel extends EventDispatcher
{
    //Universal INFO
    public var generalInfo:GeneralInfoModel;
    public var meetingVO:MeetingVO = new MeetingVO();
    public var meetingId:String;

    public var bulletinBoardLiveMembers:ArrayCollection = new ArrayCollection();

    public var xmppServices:XMPPServices;

    public var declinedParticipantsGroup:ArrayCollection = new ArrayCollection();
    public var notJoinedParticipantsGroup:ArrayCollection = new ArrayCollection();
    public var conferenceRoomParticipantsGroup:ArrayCollection = new ArrayCollection();
    public var otherLocationParticipantsGroup:ArrayCollection = new ArrayCollection();

    [Bindable]
    public var documentList:ArrayCollection = new ArrayCollection();

    [BIndable]
    public var newAttachmentList:ArrayCollection = new ArrayCollection();
    public var docsAndAttachmentsList:ArrayCollection = new ArrayCollection();

    public var bulletinBoardMsgList:ArrayCollection = new ArrayCollection();

    private var _participantList:ArrayCollection = new ArrayCollection();
    public var dismissedMeetingIDs:Array = [];
    public var visibleToastWindows:Array = [];

    public function MeetingInfoModel()
    {
        generalInfo = GeneralInfoModel.getInstance();
        xmppServices = XMPPServices.getInstance();
        _participantList.addEventListener(CollectionEvent.COLLECTION_CHANGE, allParticipantsChangeHandler);
        bulletinBoardLiveMembers.addEventListener(CollectionEvent.COLLECTION_CHANGE, bulletinBoardLiveMembersChangeHandler);
        docsAndAttachmentsList.addEventListener( CollectionEvent.COLLECTION_CHANGE, updateDocsCounter );
    }

    private static var model:MeetingInfoModel = null;

    public static function getInstance():MeetingInfoModel
    {
        if (model == null)
        {
            model = new MeetingInfoModel();
        }
        return model;
    }

    /** 
     * The handler for the collection change event of the docsAndAttachmentsList collection. 
     * 
     * We use it to manually update the counter on the Docs tab.
     */
    private function updateDocsCounter( event:CollectionEvent ):void
    {           
        var eventObj:CustomEvent = new CustomEvent( CustomEvent.UPDATE_DOC_COUNTER );
        dispatchEvent( eventObj );
    }

    public function displayToastForThisMeeting(meetingID:Number):Boolean
    {
        //trace("model::meetingID = " + meetingID);
        var doDisplayToast:Boolean = false;
        var containsMeetingID:Boolean = false;
        //the first one
        if(dismissedMeetingIDs.length == 0)
        {
            //trace("dismissedMeetingIDs.length = 0");
            doDisplayToast = true;
            dismissedMeetingIDs.push(meetingID);
        }
        else
        {
            for(var i:int=0; i < dismissedMeetingIDs.length; i++)
            {
                //trace("dismissedMeetingIDs[" + i + "] = " + dismissedMeetingIDs[i]);
                if(meetingID == dismissedMeetingIDs[i])
                {   //this one has already been dismissed
                    doDisplayToast = false;
                    containsMeetingID = true;
                    break;
                }
                else
                {
                    doDisplayToast = true;
                    containsMeetingID = false;
                }
            }

            if(containsMeetingID == false)
            {
                dismissedMeetingIDs.push(meetingID);
            }
        }
        return doDisplayToast;
    }

}

这是我的 MXML 组件(其基类是 Group)中的一些代码:

import com.fmr.transporter.controller.TransporterController;
        import com.fmr.transporter.events.CustomEvent;
        import com.fmr.transporter.model.MeetingInfoModel;
        import com.fmr.transporter.model.TransporterModel;

        import mx.collections.ArrayCollection;
        import mx.core.FlexGlobals;
        import mx.events.CollectionEvent;
        import mx.events.FlexEvent;

        private var controller:TransporterController;

        [Bindable] public var newAttachmentsList:ArrayCollection;

        [Bindable] public var meetingInfo:MeetingInfoModel;

        private function complete():void
        {
            controller = TransporterController.getInstance();

            addListeners();
        }

        /** Add listeners to this class.
         */
        private function addListeners():void{
            MeetingInfo.getInstance().docsAndAttachmentsList.addEventListener( CollectionEvent.COLLECTION_CHANGE, updateDocsCounter );
        }
4

2 回答 2

2

您扩展了事件类。默认情况下,flex 事件不会冒泡。您需要修改您的 CustomEvent 类构造函数,如下所示:

public function CustomEvent(type:String){
    super(type, true, true);
}

这将使您的事件冒泡,并且每个 flex 事件框架也可以取消。@The_asMan 正确地告诉您如何处理非冒泡事件,但我认为您可能不小心错过了正确的对象。让它起泡,你会听到的!!

于 2012-11-06T03:51:43.153 回答
0

@LondonDrugs_MediaServices、@Flextras 和 @The_asMan。

这些人是对的;为了监听事件,该类似乎不必在显示列表中。只要您对调度事件的类有正确的目标,就可以了。

但是,出于某种原因,我做了一些非常奇怪的事情,最终无法完全弄清楚。解决方案是对我在模型中更新的集合使用绑定,以更新我想要的组件。

谢谢大家的非常有帮助的评论。

于 2012-11-13T15:18:29.823 回答