actionscript 3 - How to remove a event listener with an unknown function in as3? -


my idea in small upload software use same object tasks (defined before), add , remove events , make requests, since parameters same (same method, same url...).

any time request completed, remove listeners same object can used again.

the problem when error occurs, listener call the function ioerror, don't know function should called instead if there no error:

private function ioerror(e:ioerrorevent){                 e.target.removeeventlistener(event.complete, unknownfuncion);                 e.target.removeeventlistener(ioerrorevent.io_error, ioerror);                 msg("error somewhere ("+e.text+")");             } 

how name of "unknownfunction" ? fear leave events behind...

you set couple of simple classes manage collection of event listeners. lets call collection eventbatch, this:

public class eventbatch {      private var _items:vector.<eventbatchitem> = new <eventbatchitem>[];       public function addlistener(target:ieventdispatcher, type:string, callback:function):void     {         var item:eventbatchitem = new eventbatchitem(target, type, callback);         _items.push(item);          target.addeventlistener(type, callback);     }       public function removeall():void     {         each(var i:eventbatchitem in _items)         {             i.target.removeeventlistener(i.type, i.callback);             i.dispose();         }          _items = new <eventbatchitem>[];     }  } 

and here's accompanying model represent item:

internal class eventbatchitem {      private var _target:ieventdispatcher;     private var _type:string;     private var _callback:function;       public function eventbatchitem(target:ieventdispatcher, type:string, callback:function)     {         _target = target;         _type = type;         _callback = callback;     }       internal function dispose():void     {         _target = null;         _callback = null;     }       internal function target():ieventdispatcher{ return _target; }     internal function type():string{ return _type; }     internal function callback():function{ return _callback; }  } 

this way, can add event listeners this:

var batch:eventbatch = new eventbatch(); batch.addlistener(urlloader, event.complete, completehandler); batch.addlistener(urlloader, securityerrorevent.security_error, securityerrorhandler); batch.addlistener(urlloader, ioerrorevent.io_error, ioerrorhandler); 

and in of listener functions, use .removeall() method:

batch.removeall(); 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -