extjs中addEvents中那个:true参数含义

[code="java"]
Employee = function(name){
this.name = name;
this.addEvents({
"fired" : true,
"quit" : true
});
}
Ext.extend(Employee, Ext.util.Observable);[/code]

查了很多资料 api也查了:

就是不知道

"fired" : true 的 :true 表示什么?

1个回答

[quote] this.addEvents({

"fired" : true,

"quit" : true

}); [/quote]

addEvents 这个Function是继承自 Observable类;

可以查看Observable类的addEvents()定义:

[code="java"] /**
* Used to define events on this Observable
* @param {Object} object The object with the events defined
*/
addEvents : function(o){
if(!this.events){
this.events = {};
}
if(typeof o == 'string'){
for(var i = 0, a = arguments, v; v = a[i]; i++){
if(!this.events[a[i]]){
this.events[a[i]] = true;
}
}
}else{
Ext.applyIf(this.events, o);
}
},[/code]

[quote][color=red]Ext.applyIf(this.events, o);[/color][/quote]

这里可以看到 fired,quit 属性被复制到了this.events队列;
说明,这两个属性属于event的内容;

再看到Observable类的addListener()方法
[code="java"]addListener : function(eventName, fn, scope, o){
if(typeof eventName == "object"){
o = eventName;
for(var e in o){
if(this.filterOptRe.test(e)){
continue;
}
if(typeof o[e] == "function"){
// shared options
this.addListener(e, o[e], o.scope, o);
}else{
// individual options
this.addListener(e, o[e].fn, o[e].scope, o[e]);
}
}
return;
}
o = (!o || typeof o == "boolean") ? {} : o;
eventName = eventName.toLowerCase();
var ce = this.events[eventName] || true;
if(typeof ce == "boolean"){
ce = new Ext.util.Event(this, eventName);
this.events[eventName] = ce;
}
ce.addListener(fn, scope, o);[/code]

可以看到如下代码被引用
[quote][color=red]

o = (!o || typeof o == "boolean") ? {} : o;

eventName = eventName.toLowerCase();
var ce = this.events[eventName] || true;
if(typeof ce == "boolean"){
ce = new Ext.util.Event(this, eventName);
this.events[eventName] = ce;
}
[/color][/quote]

就是最终该值,最后被转换成事件对象.被添加了事件处理函数上;

我的理解:
[quote]this.addEvents({

"fired" : true,

"quit" : true

}); [/quote]
设置成false;将不会被处理;
设置成function;将直接被当作Listener方法处理;

上面的参照EXT2.2.. 一起学习了.

Lixh1986
6891hxiL 非常感谢!
大约 7 年之前 回复
立即提问
相关内容推荐