开发者

flex 4: catch all events from a specific object

开发者 https://www.devze.com 2023-01-05 23:32 出处:网络
Is there a wa开发者_C百科y to catch all events from a specific object? When I use addEventListener() I must provide the event type. Is there a way to catch them all?

Is there a wa开发者_C百科y to catch all events from a specific object?

When I use addEventListener() I must provide the event type. Is there a way to catch them all?

Thanks!


You can get a list of all declared events using describeType. You'll need to look at parent classes to get inherited events and you'll probably want to avoid frameConstructed, enterFrame and exitFrame.

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
    <mx:Script><![CDATA[
        import flash.utils.describeType;
        import flash.utils.getDefinitionByName;

        private function init():void {

            var eventNames:Array = new Array();

            findEventsRecursive(btn, eventNames);

            for each(var eventName:String in eventNames) {
                if (eventName != "frameConstructed" &&
                    eventName != "exitFrame" &&
                    eventName != "enterFrame") {

                    btn.addEventListener(eventName, genericHandler);
                }
            }
        }

        private function genericHandler(event:Event) {
            trace(event.type + " triggered");
        }
        private function findEventsRecursive(instance:Object, eventNames:Array) {
            var description:XML = describeType(instance);

            findEvents(description, eventNames);

            for each(var parentType:String in description.extendsClass.@type) {
                var parentClass:Object = getDefinitionByName(parentType);
                var parentDescription:XML = describeType(parentClass);
                findEvents(parentDescription, eventNames);
            }
        }

        private function findEvents(description:XML, eventNames:Array) {
            for each(var eventName:XML in description.factory.metadata.(@name=='Event').arg.(@key=='name')) {
                eventNames.push(eventName.@value);
            }
        }


]]></mx:Script>

    <mx:Button id="btn" />
</mx:Application>

But events don't have to be declared. Any class can send events using string names and those will not get picked up by describeType (or through any other mechanism).

0

精彩评论

暂无评论...
验证码 换一张
取 消