How to remove event listeners correctly in this function
public function doAddItem(itemConcepto:int):void{
modalCfd = new modalCFDItems();
modalCfd.addEventListener(Event.CLOSE, closeHandler);
modalCfd.addEventListener("addItemResult", addItem_resultHandler);
modalCfd.addEventListener("addItemResize", addItemResize_resultHandler);
PopUpManager.addPopUp(modalCfd, this, true)
PopUpManager.centerPopUp(modalCfd);
}
private function addItem_resultHandler(event:Event):void{
addItemStuff();
PopUpManager.removePopUp(modalCfd);
removeListeners("modalCfd");
}
private function closeHandler(event:CloseEvent):void{
PopUpManager.removePopUp(modalCfd);
removeListeners("modalCfd");
}
public function removeListeners(listener:String):void{
if(listener == "modalCfd"){
modalCfd.removeEventListener("addItemResult", addItem_resultHandler);
modalCfd.removeEventListener("addItemResize", addItemResize_resultHandler);
modalCfd.开发者_StackOverflowremoveEventListener(Event.CLOSE , closeHandler);
}
}
As you can see I'm a newbie with good practices (in pretty much anything) How can I remove the event listeners correctly? That's a Pop Up I'm handling there. Sorry if that function doesn't make much sense, I simplified it to fit stackoverflow.
You already have a reference to your object so just use that in your removelisteners function.
public function doAddItem(itemConcepto:int):void{
modalCfd = new modalCFDItems();
modalCfd.addEventListener(Event.CLOSE, closeHandler);
modalCfd.addEventListener("addItemResult", addItem_resultHandler);
modalCfd.addEventListener("addItemResize", addItemResize_resultHandler);
PopUpManager.addPopUp(modalCfd, this, true)
PopUpManager.centerPopUp(modalCfd);
}
private function addItem_resultHandler(event:Event):void{
addItemStuff();
PopUpManager.removePopUp(modalCfd);
removeListeners();
}
private function closeHandler(event:CloseEvent):void{
PopUpManager.removePopUp(modalCfd);
removeListeners();
}
public function removeListeners():void{
modalCfd.removeEventListener("addItemResult", addItem_resultHandler);
modalCfd.removeEventListener("addItemResize", addItemResize_resultHandler);
modalCfd.removeEventListener(Event.CLOSE , closeHandler);
}
精彩评论