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,
> >>>
> >>> 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
>


Hi Juan,

thanks for the solution. I think it's the only way monitoring the register
of listeners. I thought as3 could have a kind of smart method returning all
registered methods for each object. But your solution is more smarter than
flash.

Thanks man,

-- 
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

Reply via email to