ActionScript 3 / Flex 3 - Adding custom events to a class
ActionScript 3 / Flex 3 - 向类添加自定义事件
Say I have the following Event:
说我有以下事件:
import flash.events.Event;
public class SomeEvent extends Event
{
public static const EVENT_ACTION:String = "eventAction";
public function SomeEvent(type:String) {
super(type);
}
override public function clone():Event {
return new SomeEvent(this.type);
}
}
... and the following Class:
......以及以下课程:
public class SomeClass extends EventDispatcher
{
public function someFunction():void
{
dispatchEvent(new SomeEvent("eventAction"));
}
}
What is the best way to show that 'SomeClass' throws 'SomeEvent'? The only way I have found is to decorate 'SomeClass' with the [Event] attribute, as follows:
显示'SomeClass'抛出'SomeEvent'的最佳方式是什么?我找到的唯一方法是使用[Event]属性修饰'SomeClass',如下所示:
[Event (name="eventAction", type="SomeEvent")]
This allows me to instantiate the class and add an event listener by doing this:
这允许我实例化类并通过执行以下操作添加事件侦听器:
var someClassInstance:SomeClass = new SomeClass();
someClassInstance.addEventListener(SomeEvent.EVENT_ACTION, mycallbackmethod);
Is there a better way to do this? Putting the [Event] attribute on the class followed by some string literals just feels ... wrong. Thanks in advance for the help!
有一个更好的方法吗?将[Event]属性放在类后跟一些字符串文字只是感觉......错了。在此先感谢您的帮助!
2 个解决方案
#1
You're doing it right. Currently, the AS3 compiler allows only string literals in metadata. Constants cannot be used.
你做得对。目前,AS3编译器仅允许元数据中的字符串文字。常量不能使用。
By the way, Adobe's public bug database has a feature request to allow ActionScript constants in metadata. Feel free to vote for it.
顺便说一下,Adobe的公共错误数据库有一个功能请求,允许在元数据中使用ActionScript常量。随意投票。
#2
I know, I feel the same way; using the string literals does seem a bit wrong somehow. But to the best of my knowledge, from all the documentation I've seen and the talks I've attended and in reading the Flex source code, etc., it does appear that's the proper way to do it.
我知道,我也有同感;使用字符串文字确实看起来有点不对劲。但据我所知,从我见过的所有文档和我参加过的演讲以及阅读Flex源代码等,看来这是正确的方法。
#1
You're doing it right. Currently, the AS3 compiler allows only string literals in metadata. Constants cannot be used.
你做得对。目前,AS3编译器仅允许元数据中的字符串文字。常量不能使用。
By the way, Adobe's public bug database has a feature request to allow ActionScript constants in metadata. Feel free to vote for it.
顺便说一下,Adobe的公共错误数据库有一个功能请求,允许在元数据中使用ActionScript常量。随意投票。
#2
I know, I feel the same way; using the string literals does seem a bit wrong somehow. But to the best of my knowledge, from all the documentation I've seen and the talks I've attended and in reading the Flex source code, etc., it does appear that's the proper way to do it.
我知道,我也有同感;使用字符串文字确实看起来有点不对劲。但据我所知,从我见过的所有文档和我参加过的演讲以及阅读Flex源代码等,看来这是正确的方法。