`我的“模型”是一个扩展 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 );
但它永远不会到达这个听众。
我遇到过很多这样的问题,我认为事件过程中有一个我不理解的关键部分。
任何人都可以提供任何有用的线索吗?
谢谢!
更新:
回应以下所有评论(感谢您的所有回复!):
- 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 );
}