Re: [Flashcoders] removing all listeners from a dpo
No worries. Personally, I think in most cases using this approach would not save you much work (in case it works in the first place). But I agree that it would be useful that the EventDispatcher class exposed a method to remove all listeners and probably a method to query any EventDispatcher object about all its registered listeners (maybe returning an array of some read only value objects / data structures, to prevent accidental inconsistencies). You have the hasEventListener method, but you have to know what events to look for before hand, so in this scenario it's not of much help, I guess. Cheers Juan Pablo Califano 2008/7/21, Fabio Pinatti <[EMAIL PROTECTED]>: > > On Sun, Jul 20, 2008 at 7:40 PM, Juan Pablo Califano < > [EMAIL PROTECTED]> wrote: > > > PD2: > > > > this line should be removed from the unregisterEventListener() > > > > _dispatcher.removeEventListener(type, listener, useCapture); > > > > since it will cause an infinite loop and the eventListener has already > been > > removed in the caller anyway. > > > > So, to sum up: > > > > public function unregisterEventListener(type:String, listener:Function, > > useCapture:Boolean):void { > > > >var i:int = _eventsList.length - 1; > > > > var cur:EventListenerData; > > > > while (i >= 0) { > > cur = _eventsList[i] as EventListenerData; > >if (cur.type == type && cur.listener == listener && cur.useCapture == > > useCapture) { > >// _dispatcher.removeEventListener(type, listener, useCapture); > > _eventsList.splice(i, 1); > >} > >i--; > >} > > } > > > > > > Cheers > > Juan Pablo Califano > > > > > > 2008/7/20, Juan Pablo Califano <[EMAIL PROTECTED]>: > > > > > > PD: > > > > > > I forgot to actually remove the eventListenerData from the list in the > > > unregisterEventListener method of the Bookkeeper class. Looping > backwards > > > and adding _eventsList.splice(i, 1); should do it. > > > > > > > > > Cheers > > > Juan Pablo Califano > > > > > > > > > > > > 2008/7/20, Juan Pablo Califano <[EMAIL PROTECTED]>: > > >> > > >> Hi, > > >> > > >> You could try to do your own bookkeeping, so you know what listeners > are > > >> registered to an EventDispatcher object. You can have one class to > hold > > that > > >> info in a list and write a method that loops through that list and > > remove > > >> all registered listeners. You'd still have to override the > > addEventListener > > >> and removeEventListener methods in the dispatcher object, to intercept > > those > > >> calls from external code and keep your bookkeeper in sync. > > >> > > >> Some code to illustrate it (keep mind that even though it compiles, > this > > >> is untested and may be not the best idea). > > >> > > >> package > > >> { > > >> import flash.display.Sprite; > > >> > > >> public class Main extends Sprite > > >> { > > >> private var _evtBookKeeper:EventBookkeeper; > > >> > > >> public function Main():void > > >> { > > >>_evtBookKeeper = new EventBookkeeper(this); > > >> } > > >> > > >> override public function addEventListener(type:String, > > >> listener:Function, useCapture:Boolean = false, priority:int = 0, > > >> useWeakReference:Boolean = false):void > > >> { > > >>super.addEventListener(type, listener, useCapture, priority, > > >> useWeakReference); > > >>_evtBookKeeper.registerEventListener(type, listener, useCapture); > > >> } > > >> > > >> override public function removeEventListener(type:String, > > >> listener:Function, useCapture:Boolean = false):void > > >> { > > >>super.removeEventListener(type, listener, useCapture); > > >>_evtBookKeeper.unregisterEventListener(type, listener, useCapture); > > >> } > > >> > > >> public function removeAllListeners():void { > > >>_evtBookKeeper.unregisterAndRemoveAll(); > > >> } > > >> > > >> } > > >> } > > >> > > >> // > > >> // > > >> // > > >> > > >> package > > >> { > > >> import flash.events.IEventDispatcher; > > >> import flash.utils.Dictionary; > > >> > > >> public class EventBookkeeper > > >> { > > >> > > >> private var _eventsList:Array; > > >> private var _dispatcher:IEventDispatcher; > > >> > > >> public function EventBookkeeper(dispatcher:IEventDispatcher) { > > >>_eventsList = []; > > >> } > > >> > > >> public function registerEventListener(type:String, > listener:Function, > > >> useCapture:Boolean):void { > > >>_eventsList.push(new EventListenerData(type,listener,useCapture)); > > >> } > > >> > > >> public function unregisterEventListener(type:String, > > listener:Function, > > >> useCapture:Boolean):void { > > >>var len:int = _eventsList.length; > > >>var i:int = 0; > > >> > > >>var cur:EventListenerData; > > >> > > >>while (i < len) { > > >> cur = _eventsList[i] as EventListenerData; > > >> if (cur.type == type && cur.listener == listener && cur.useCapture > > == > > >> useCapture) { > > >> _dispatcher.removeEventListener(type, listener, useCapture); > > >> } > > >> i
Re: [Flashcoders] removing all listeners from a dpo
On Sun, Jul 20, 2008 at 7:40 PM, Juan Pablo Califano < [EMAIL PROTECTED]> wrote: > PD2: > > this line should be removed from the unregisterEventListener() > > _dispatcher.removeEventListener(type, listener, useCapture); > > since it will cause an infinite loop and the eventListener has already been > removed in the caller anyway. > > So, to sum up: > > public function unregisterEventListener(type:String, listener:Function, > useCapture:Boolean):void { > >var i:int = _eventsList.length - 1; > > var cur:EventListenerData; > > while (i >= 0) { > cur = _eventsList[i] as EventListenerData; >if (cur.type == type && cur.listener == listener && cur.useCapture == > useCapture) { >// _dispatcher.removeEventListener(type, listener, useCapture); > _eventsList.splice(i, 1); >} >i--; >} > } > > > Cheers > Juan Pablo Califano > > > 2008/7/20, Juan Pablo Califano <[EMAIL PROTECTED]>: > > > > PD: > > > > I forgot to actually remove the eventListenerData from the list in the > > unregisterEventListener method of the Bookkeeper class. Looping backwards > > and adding _eventsList.splice(i, 1); should do it. > > > > > > Cheers > > Juan Pablo Califano > > > > > > > > 2008/7/20, Juan Pablo Califano <[EMAIL PROTECTED]>: > >> > >> Hi, > >> > >> You could try to do your own bookkeeping, so you know what listeners are > >> registered to an EventDispatcher object. You can have one class to hold > that > >> info in a list and write a method that loops through that list and > remove > >> all registered listeners. You'd still have to override the > addEventListener > >> and removeEventListener methods in the dispatcher object, to intercept > those > >> calls from external code and keep your bookkeeper in sync. > >> > >> Some code to illustrate it (keep mind that even though it compiles, this > >> is untested and may be not the best idea). > >> > >> package > >> { > >> import flash.display.Sprite; > >> > >> public class Main extends Sprite > >> { > >> private var _evtBookKeeper:EventBookkeeper; > >> > >> public function Main():void > >> { > >>_evtBookKeeper = new EventBookkeeper(this); > >> } > >> > >> override public function addEventListener(type:String, > >> listener:Function, useCapture:Boolean = false, priority:int = 0, > >> useWeakReference:Boolean = false):void > >> { > >>super.addEventListener(type, listener, useCapture, priority, > >> useWeakReference); > >>_evtBookKeeper.registerEventListener(type, listener, useCapture); > >> } > >> > >> override public function removeEventListener(type:String, > >> listener:Function, useCapture:Boolean = false):void > >> { > >>super.removeEventListener(type, listener, useCapture); > >>_evtBookKeeper.unregisterEventListener(type, listener, useCapture); > >> } > >> > >> public function removeAllListeners():void { > >>_evtBookKeeper.unregisterAndRemoveAll(); > >> } > >> > >> } > >> } > >> > >> // > >> // > >> // > >> > >> package > >> { > >> import flash.events.IEventDispatcher; > >> import flash.utils.Dictionary; > >> > >> public class EventBookkeeper > >> { > >> > >> private var _eventsList:Array; > >> private var _dispatcher:IEventDispatcher; > >> > >> public function EventBookkeeper(dispatcher:IEventDispatcher) { > >>_eventsList = []; > >> } > >> > >> public function registerEventListener(type:String, listener:Function, > >> useCapture:Boolean):void { > >>_eventsList.push(new EventListenerData(type,listener,useCapture)); > >> } > >> > >> public function unregisterEventListener(type:String, > listener:Function, > >> useCapture:Boolean):void { > >>var len:int = _eventsList.length; > >>var i:int = 0; > >> > >>var cur:EventListenerData; > >> > >>while (i < len) { > >> cur = _eventsList[i] as EventListenerData; > >> if (cur.type == type && cur.listener == listener && cur.useCapture > == > >> useCapture) { > >> _dispatcher.removeEventListener(type, listener, useCapture); > >> } > >> i++; > >>} > >> } > >> > >> public function unregisterAndRemoveAll():void { > >>var len:int = _eventsList.length; > >>var i:int = 0; > >> > >>var cur:EventListenerData; > >> > >>while (i < len) { > >> cur = _eventsList[i] as EventListenerData; > >> _dispatcher.removeEventListener(cur.type, cur.listener, > >> cur.useCapture); > >> i++; > >>} > >> > >>_eventsList = []; > >> > >> } > >> } > >> > >> } > >> > >> // > >> // > >> // > >> > >> package > >> { > >> > >> public class EventListenerData > >> { > >> > >> public var type:String; > >> public var listener:Function; > >> public var useCapture:Boolean; > >> > >> public function > >> EventListenerData(type:String,listener:Function,useCapture:Boolean) { > >>this.type = type; > >>this.listener = listener; > >>this.useCapture = useCapture; > >> } > >> > >> } > >> > >> } > >> > >> > >> 2008/7/18, Fabio Pinatti <[EMAIL PROTECTED]>: > >>> > >>> Hi list,
Re: [Flashcoders] removing all listeners from a dpo
PD2: this line should be removed from the unregisterEventListener() _dispatcher.removeEventListener(type, listener, useCapture); since it will cause an infinite loop and the eventListener has already been removed in the caller anyway. So, to sum up: public function unregisterEventListener(type:String, listener:Function, useCapture:Boolean):void { var i:int = _eventsList.length - 1; var cur:EventListenerData; while (i >= 0) { cur = _eventsList[i] as EventListenerData; if (cur.type == type && cur.listener == listener && cur.useCapture == useCapture) { // _dispatcher.removeEventListener(type, listener, useCapture); _eventsList.splice(i, 1); } i--; } } Cheers Juan Pablo Califano 2008/7/20, Juan Pablo Califano <[EMAIL PROTECTED]>: > > PD: > > I forgot to actually remove the eventListenerData from the list in the > unregisterEventListener method of the Bookkeeper class. Looping backwards > and adding _eventsList.splice(i, 1); should do it. > > > Cheers > Juan Pablo Califano > > > > 2008/7/20, Juan Pablo Califano <[EMAIL PROTECTED]>: >> >> Hi, >> >> You could try to do your own bookkeeping, so you know what listeners are >> registered to an EventDispatcher object. You can have one class to hold that >> info in a list and write a method that loops through that list and remove >> all registered listeners. You'd still have to override the addEventListener >> and removeEventListener methods in the dispatcher object, to intercept those >> calls from external code and keep your bookkeeper in sync. >> >> Some code to illustrate it (keep mind that even though it compiles, this >> is untested and may be not the best idea). >> >> package >> { >> import flash.display.Sprite; >> >> public class Main extends Sprite >> { >> private var _evtBookKeeper:EventBookkeeper; >> >> public function Main():void >> { >>_evtBookKeeper = new EventBookkeeper(this); >> } >> >> override public function addEventListener(type:String, >> listener:Function, useCapture:Boolean = false, priority:int = 0, >> useWeakReference:Boolean = false):void >> { >>super.addEventListener(type, listener, useCapture, priority, >> useWeakReference); >>_evtBookKeeper.registerEventListener(type, listener, useCapture); >> } >> >> override public function removeEventListener(type:String, >> listener:Function, useCapture:Boolean = false):void >> { >>super.removeEventListener(type, listener, useCapture); >>_evtBookKeeper.unregisterEventListener(type, listener, useCapture); >> } >> >> public function removeAllListeners():void { >>_evtBookKeeper.unregisterAndRemoveAll(); >> } >> >> } >> } >> >> // >> // >> // >> >> package >> { >> import flash.events.IEventDispatcher; >> import flash.utils.Dictionary; >> >> public class EventBookkeeper >> { >> >> private var _eventsList:Array; >> private var _dispatcher:IEventDispatcher; >> >> public function EventBookkeeper(dispatcher:IEventDispatcher) { >>_eventsList = []; >> } >> >> public function registerEventListener(type:String, listener:Function, >> useCapture:Boolean):void { >>_eventsList.push(new EventListenerData(type,listener,useCapture)); >> } >> >> public function unregisterEventListener(type:String, listener:Function, >> useCapture:Boolean):void { >>var len:int = _eventsList.length; >>var i:int = 0; >> >>var cur:EventListenerData; >> >>while (i < len) { >> cur = _eventsList[i] as EventListenerData; >> if (cur.type == type && cur.listener == listener && cur.useCapture == >> useCapture) { >> _dispatcher.removeEventListener(type, listener, useCapture); >> } >> i++; >>} >> } >> >> public function unregisterAndRemoveAll():void { >>var len:int = _eventsList.length; >>var i:int = 0; >> >>var cur:EventListenerData; >> >>while (i < len) { >> cur = _eventsList[i] as EventListenerData; >> _dispatcher.removeEventListener(cur.type, cur.listener, >> cur.useCapture); >> i++; >>} >> >>_eventsList = []; >> >> } >> } >> >> } >> >> // >> // >> // >> >> package >> { >> >> public class EventListenerData >> { >> >> public var type:String; >> public var listener:Function; >> public var useCapture:Boolean; >> >> public function >> EventListenerData(type:String,listener:Function,useCapture:Boolean) { >>this.type = type; >>this.listener = listener; >>this.useCapture = useCapture; >> } >> >> } >> >> } >> >> >> 2008/7/18, Fabio Pinatti <[EMAIL PROTECTED]>: >>> >>> Hi list, >>> >>> I'm wondering if I can have a generical function, that I can remove all >>> listeners registered for an object. Imagine a button with 10 listeners, >>> and >>> I don`t need know which ones it are to remove. I simply call >>> "removeListeners(dpo)" and that function does all work. >>> >>> Is there any way? >>> >>> Thanks so much, >>> >>> -- >>> Fábio Pinatti >>> :: web.developer >>> www.pinatti.com.br >>> :: 19. 9184.3745 / 3342.
Re: [Flashcoders] removing all listeners from a dpo
PD: I forgot to actually remove the eventListenerData from the list in the unregisterEventListener method of the Bookkeeper class. Looping backwards and adding _eventsList.splice(i, 1); should do it. Cheers Juan Pablo Califano 2008/7/20, Juan Pablo Califano <[EMAIL PROTECTED]>: > > Hi, > > You could try to do your own bookkeeping, so you know what listeners are > registered to an EventDispatcher object. You can have one class to hold that > info in a list and write a method that loops through that list and remove > all registered listeners. You'd still have to override the addEventListener > and removeEventListener methods in the dispatcher object, to intercept those > calls from external code and keep your bookkeeper in sync. > > Some code to illustrate it (keep mind that even though it compiles, this is > untested and may be not the best idea). > > package > { > import flash.display.Sprite; > > public class Main extends Sprite > { > private var _evtBookKeeper:EventBookkeeper; > > public function Main():void > { >_evtBookKeeper = new EventBookkeeper(this); > } > > override public function addEventListener(type:String, listener:Function, > useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = > false):void > { >super.addEventListener(type, listener, useCapture, priority, > useWeakReference); >_evtBookKeeper.registerEventListener(type, listener, useCapture); > } > > override public function removeEventListener(type:String, > listener:Function, useCapture:Boolean = false):void > { >super.removeEventListener(type, listener, useCapture); >_evtBookKeeper.unregisterEventListener(type, listener, useCapture); > } > > public function removeAllListeners():void { >_evtBookKeeper.unregisterAndRemoveAll(); > } > > } > } > > // > // > // > > package > { > import flash.events.IEventDispatcher; > import flash.utils.Dictionary; > > public class EventBookkeeper > { > > private var _eventsList:Array; > private var _dispatcher:IEventDispatcher; > > public function EventBookkeeper(dispatcher:IEventDispatcher) { >_eventsList = []; > } > > public function registerEventListener(type:String, listener:Function, > useCapture:Boolean):void { >_eventsList.push(new EventListenerData(type,listener,useCapture)); > } > > public function unregisterEventListener(type:String, listener:Function, > useCapture:Boolean):void { >var len:int = _eventsList.length; >var i:int = 0; > >var cur:EventListenerData; > >while (i < len) { > cur = _eventsList[i] as EventListenerData; > if (cur.type == type && cur.listener == listener && cur.useCapture == > useCapture) { > _dispatcher.removeEventListener(type, listener, useCapture); > } > i++; >} > } > > public function unregisterAndRemoveAll():void { >var len:int = _eventsList.length; >var i:int = 0; > >var cur:EventListenerData; > >while (i < len) { > cur = _eventsList[i] as EventListenerData; > _dispatcher.removeEventListener(cur.type, cur.listener, > cur.useCapture); > i++; >} > >_eventsList = []; > > } > } > > } > > // > // > // > > package > { > > public class EventListenerData > { > > public var type:String; > public var listener:Function; > public var useCapture:Boolean; > > public function > EventListenerData(type:String,listener:Function,useCapture:Boolean) { >this.type = type; >this.listener = listener; >this.useCapture = useCapture; > } > > } > > } > > > 2008/7/18, Fabio Pinatti <[EMAIL PROTECTED]>: >> >> Hi list, >> >> I'm wondering if I can have a generical function, that I can remove all >> listeners registered for an object. Imagine a button with 10 listeners, >> and >> I don`t need know which ones it are to remove. I simply call >> "removeListeners(dpo)" and that function does all work. >> >> Is there any way? >> >> Thanks so much, >> >> -- >> Fábio Pinatti >> :: web.developer >> www.pinatti.com.br >> :: 19. 9184.3745 / 3342.1130 >> ___ >> Flashcoders mailing list >> Flashcoders@chattyfig.figleaf.com >> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders >> > > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] removing all listeners from a dpo
Hi, You could try to do your own bookkeeping, so you know what listeners are registered to an EventDispatcher object. You can have one class to hold that info in a list and write a method that loops through that list and remove all registered listeners. You'd still have to override the addEventListener and removeEventListener methods in the dispatcher object, to intercept those calls from external code and keep your bookkeeper in sync. Some code to illustrate it (keep mind that even though it compiles, this is untested and may be not the best idea). package { import flash.display.Sprite; public class Main extends Sprite { private var _evtBookKeeper:EventBookkeeper; public function Main():void { _evtBookKeeper = new EventBookkeeper(this); } override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void { super.addEventListener(type, listener, useCapture, priority, useWeakReference); _evtBookKeeper.registerEventListener(type, listener, useCapture); } override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void { super.removeEventListener(type, listener, useCapture); _evtBookKeeper.unregisterEventListener(type, listener, useCapture); } public function removeAllListeners():void { _evtBookKeeper.unregisterAndRemoveAll(); } } } // // // package { import flash.events.IEventDispatcher; import flash.utils.Dictionary; public class EventBookkeeper { private var _eventsList:Array; private var _dispatcher:IEventDispatcher; public function EventBookkeeper(dispatcher:IEventDispatcher) { _eventsList = []; } public function registerEventListener(type:String, listener:Function, useCapture:Boolean):void { _eventsList.push(new EventListenerData(type,listener,useCapture)); } public function unregisterEventListener(type:String, listener:Function, useCapture:Boolean):void { var len:int = _eventsList.length; var i:int = 0; var cur:EventListenerData; while (i < len) { cur = _eventsList[i] as EventListenerData; if (cur.type == type && cur.listener == listener && cur.useCapture == useCapture) { _dispatcher.removeEventListener(type, listener, useCapture); } i++; } } public function unregisterAndRemoveAll():void { var len:int = _eventsList.length; var i:int = 0; var cur:EventListenerData; while (i < len) { cur = _eventsList[i] as EventListenerData; _dispatcher.removeEventListener(cur.type, cur.listener, cur.useCapture); i++; } _eventsList = []; } } } // // // package { public class EventListenerData { public var type:String; public var listener:Function; public var useCapture:Boolean; public function EventListenerData(type:String,listener:Function,useCapture:Boolean) { this.type = type; this.listener = listener; this.useCapture = useCapture; } } } 2008/7/18, Fabio Pinatti <[EMAIL PROTECTED]>: > > Hi list, > > I'm wondering if I can have a generical function, that I can remove all > listeners registered for an object. Imagine a button with 10 listeners, and > I don`t need know which ones it are to remove. I simply call > "removeListeners(dpo)" and that function does all work. > > Is there any way? > > Thanks so much, > > -- > Fábio Pinatti > :: web.developer > www.pinatti.com.br > :: 19. 9184.3745 / 3342.1130 > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] removing all listeners from a dpo
Hi list, I'm wondering if I can have a generical function, that I can remove all listeners registered for an object. Imagine a button with 10 listeners, and I don`t need know which ones it are to remove. I simply call "removeListeners(dpo)" and that function does all work. Is there any way? Thanks so much, -- Fábio Pinatti :: web.developer www.pinatti.com.br :: 19. 9184.3745 / 3342.1130 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders