从模型广播自定义事件

时间:2012-11-05 18:53:37

标签: actionscript-3 flex

`我的"型号"是一个扩展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不是显示组件,不应负责广播事件;这是我没有得到的那件!!
  2. 这是我的代码:在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 );
            }
    

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

@LondonDrugs_MediaServices,@ Flextras和@The_asMan。

这些家伙是对的;似乎该类不必在显示列表上以便监听事件。只要你有一个调度事件的类的正确目标,它应该没问题。

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

感谢大家的非常有益的评论。

相关问题