-
-
Save Stray/709093 to your computer and use it in GitHub Desktop.
| ////////// code in a robotlegs mediator | |
| private function someHandler(e:Event):void | |
| { | |
| // whatever | |
| } | |
| // this mediator adds the handler to the relaxedEventMap | |
| override public function onRegister():void | |
| { | |
| relaxedEventMap.mapListener(SomeEvent.SOMETHING, someHandler, SomeEvent); | |
| // I would like to avoid an extra param of owner:* in this method | |
| } | |
| ////////// code in the relaxedEventMap | |
| public function mapListener(eventType:String, listener:Function, eventClass:Class):void | |
| { | |
| var listenerOwner:* = findOwner(listener); | |
| listenerMappingsByOwner[listenerOwner] = ... | |
| } | |
| private function findOwner(childFunction:Function):void | |
| { | |
| magic-code-that-gets-the-owner-goes-here | |
| return functionOwner; | |
| } | |
| // the purpose of getting the owners is so we can do | |
| public function unmapListenersFrom(owner:*):void | |
| { | |
| // remove all the mappings for this owner... | |
| } |
The relaxedEventMap is shared across multiple mediators. So - unmapListeners() as per the current system would also remove everybody else's listeners too! Of course you could unmap each individual listener in the mediator's preRemove, but I was looking for something easier.
The idea at the moment is to offer the unmapListenersFrom(owner:*) (which would be run in preRemove) and keep track of which object has registered each listener, so all the listeners for that object can be removed in one hit, leaving the rest intact. But that means adding another parameter to the mapping.
Or you could add sugar to RelaxedMediator ?
Only reason I suggest considering this, is I'm pretty certain you can't find out the class which defined the method, or the instance object that specified the closure.
Anyway, imagine two mediator instances of AMediator. what would the "owner" even be, best you could hope for is to know the "caller" to mapListener. This existed in arguments in AS2, but no more:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/arguments.html#propertySummary
Yeah - it's the calling object not the class that is needed - exactly as 'caller'. It turns out that there are ways to find it out by instantiating an Error and then parsing the stacktrace, but I think I'm just going to add the param. I was just interested to find out whether there was anything undocumented that would allow me to access the caller, or the object that had instantiated the handler.
Why not have the mediator (indirectly) unmap itself? As per existing RL mediators?