In the old code, Observable.fireEvent used fireDirect which looked like this:
See that if the handler does not explicitly return false the event firing code returns true, and if you write your Observable subclass so that handlers may veto the operation by explicitly returning false - your code will still execute if no return statement is executed.
The new code is:
It just needs "return true;" adding to the end!
YAHOO.util.CustomEvent.prototype.fireDirect = function(){
var len=this.subscribers.length;
for (var i=0; i<len; ++i) {
var s = this.subscribers[i];
if(s){
var scope = (s.override) ? s.obj : this.scope;
if(s.fn.apply(scope, arguments) === false){
return false;
}
}
}
return true;
};
The new code is:
fire : function(){
var args = Array.prototype.slice.call(arguments, 0);
var ls = this.listeners, scope;
for(var i = 0, len = ls.length; i < len; i++){
var l = ls[i];
if(l.fireFn.apply(l.scope, arguments) === false){
return false;
}
}
}

|
#2
|
|
I added it. Personally, I always check === or !== but I am paranoid.
![]() ![]() |
本文探讨了YUI3中事件处理机制的变化,从旧版的fireDirect方法过渡到新版的fire方法,并强调了返回值的重要性。作者指出,在新的事件处理机制中,如果事件处理器没有显式返回false,则默认继续执行后续逻辑。

420

被折叠的 条评论
为什么被折叠?



