Observable.fire - behaviour changed. - Ext JS

本文探讨了YUI3中事件处理机制的变化,从旧版的fireDirect方法过渡到新版的fire方法,并强调了返回值的重要性。作者指出,在新的事件处理机制中,如果事件处理器没有显式返回false,则默认继续执行后续逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In the old code, Observable.fireEvent used fireDirect which looked like this:

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;
};
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:

        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;
                }
            }
        }
It just needs "return true;" adding to the end!
Reply With Quote
  #2  
Old 02-23-2007, 09:19 AM
Default

I added it. Personally, I always check === or !== but I am paranoid.
Reply With Quote
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值