标签:
转载请注明:http://blog.csdn.net/lichwei1983
第一种方法: 直接修改WebCore的Window, HTML中关于Event名称,监听器。
用法: [ConstructorTemplate=Event]. 这种方法只能用于制定事件接口. [InitializedByEventConstructor]可以用来修饰接口的属性,举例如下:
[
ConstructorTemplate=Event
] interface MichaelIREvent {
attribute DOMString str1;
[InitializedByEventConstructor] attribute DOMString str2;
};
因为事件接口的构建器(constructor)要求特殊的绑定,我们需要使用这种方法来实现,通常的Constructor是不行的。
当把如上IDL文件加入Webkit编译以后,测试的JavaScript代码如下,首先建一个MichaelIREvent的DOM对象:
var e = new MichaelIREvent("type", { bubbles: true, cancelable: true });
然后,WebCore里的MichaelIREvent::create()就会被调用. Specifically, WebCore会自动生成如下方法并把它作为构建器的回调函数:
PassRefPtr<FooEvent> MichaelIREvent::create(const AtomicString& type, const FooEventInit& initializer)
{
...;
}
[InitializedByEventConstructor] 应该修饰那些需要构造器初始化的属性. Event接口的Spec规定了哪些属性需要初始化.具体参看W3C的链接:the spec of Event.
[Constructor(DOMStringtype, optional EventInit eventInitDict),
Exposed=(Window,Worker)]
interfaceEvent{
readonly attribute DOMString type;
readonly attribute EventTarget? target;
readonly attribute EventTarget? currentTarget;
const unsigned short NONE = 0;
const unsigned short CAPTURING_PHASE = 1;
const unsigned short AT_TARGET = 2;
const unsigned short BUBBLING_PHASE = 3;
readonly attribute unsigned short eventPhase;
void stopPropagation();
void stopImmediatePropagation();
readonly attribute boolean bubbles;
readonly attribute boolean cancelable;
void preventDefault();
readonly attribute boolean defaultPrevented;
[Unforgeable] readonly attribute boolean isTrusted;
readonly attribute DOMTimeStamp timeStamp;
void initEvent(DOMStringtype, booleanbubbles, booleancancelable);
};
dictionary EventInit{
boolean bubbles = false;
boolean cancelable = false;
};
EventInit有属性:bubbles 和 cancelable, 因此bubbles 和 cancelable唯一需要被Event构建器初始化的两个参数. 换句话说,对于自定义的事件,你只需要为bubbles和cancelable属性指定InitializedByEventConstructorIn修饰即可。
标签:
原文地址:http://blog.csdn.net/lichwei1983/article/details/43888555