如何在此函数中正确删除事件侦听器

时间:2011-08-25 21:33:13

标签: actionscript-3 flex4

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.removeEventListener(Event.CLOSE , closeHandler);
                }
            }

正如你所看到的,我是一个有良好实践的新手(几乎所有东西)我如何才能正确删除事件监听器?那是我在那里处理的Pop Up。很抱歉,如果该功能没有多大意义,我将其简化为适合stackoverflow。

1 个答案:

答案 0 :(得分:1)

您已经有了对象的引用,所以只需在removelisteners函数中使用它。

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);
            }