码迷,mamicode.com
首页 > 编程语言 > 详细

javascript事件之:jQuery.event.dispatch详解

时间:2014-12-31 09:52:35      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

  jQuery.event.dispatch,字面意思是事件分发。就是执行绑定的函数。

  在jQuery.event.add中,

//1, jQuery.event.add中
jQuery.event.dispatch.apply( eventHandle.elem, arguments )
//2, jQuery.event.simulate中
jQuery.event.dispatch.call( elem, e );

  我们来看看jQuery.event.dispatch的源码,看看其实现了什么。

dispatch: function( event ) {

    // 修正事件源对象
    event = jQuery.event.fix( event );
    var i, j, ret, matched, handleObj,
        handlerQueue = [],
        args = core_slice.call( arguments ),
        //得到绑定的事件对象
        handlers = ( data_priv.get( this, "events" ) || {} )[ event.type ] || [],
        special = jQuery.event.special[ event.type ] || {};
    args[0] = event;
    event.delegateTarget = this;
    // Call the preDispatch hook for the mapped type, and let it bail if desired
    if ( special.preDispatch && special.preDispatch.call( this, event ) === false ) {
        return;
    }

    // 得到handler的执行队列
    handlerQueue = jQuery.event.handlers.call( this, event, handlers );

    // Run delegates first; they may want to stop propagation beneath us
    i = 0;
    while ( (matched = handlerQueue[ i++ ]) && !event.isPropagationStopped() ) {
        event.currentTarget = matched.elem;

        j = 0;
        while ( (handleObj = matched.handlers[ j++ ]) && !event.isImmediatePropagationStopped() ) {

            //不存在命名空间,或者匹配的命名空间
            if ( !event.namespace_re || event.namespace_re.test( handleObj.namespace ) ) {

                event.handleObj = handleObj;
                event.data = handleObj.data;
                //执行绑定函数
                ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler )
                        .apply( matched.elem, args );
                // 如果是return false, 阻止默认事件,阻止冒泡
                if ( ret !== undefined ) {
                    if ( (event.result = ret) === false ) {
                        event.preventDefault();
                        event.stopPropagation();
                    }
                }
            }
        }
    }

    // Call the postDispatch hook for the mapped type
    if ( special.postDispatch ) {
        special.postDispatch.call( this, event );
    }
    
    return event.result;
},

 

  

javascript事件之:jQuery.event.dispatch详解

标签:

原文地址:http://www.cnblogs.com/pfzeng/p/4195045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!