zoukankan      html  css  js  c++  java
  • yii2阅读随笔14

    继续来看Event.php

     /**
         * Triggers a class-level event.
         * 触发类级别事件。
         * This method will cause invocation of event handlers that are attached to the named event
         * for the specified class and all its parent classes.
         * 触发某个类或者对象的某个事件
         * @param string|object $class the object or the fully qualified class name specifying the class-level event.
         * @param string $name the event name.
         * @param Event $event the event parameter. If not set, a default [[Event]] object will be created.
         */
        public static function trigger($class, $name, $event = null)
        {
            if (empty(self::$_events[$name])) {
                return;
            }
            if ($event === null) {
                // 事件不存在,就创建一个 Event 对象
                $event = new static;
            }
            // 设置event对象的属性,默认是未被处理的
            $event->handled = false;
            $event->name = $name;
            if (is_object($class)) {
                if ($event->sender === null) {
                    // 如果 $class 是个对象,并且是 sender 为空,就将 $class 赋给 sender,即 $class 就是触发事件的对象
                    $event->sender = $class;
                }
                $class = get_class($class);
            } else {
                $class = ltrim($class, '\');
            }
            // 循环类的 $_event,直到遇到 $event->handled 为真或者没有父类了为止
            do {
                if (!empty(self::$_events[$name][$class])) {
                    foreach (self::$_events[$name][$class] as $handler) {
                        // 将参数赋到 event 对象的 data 属性上
                        $event->data = $handler[1];
                        // 调用 $handler 方法
                        // 在方法中,可以用 $this->data 取到相应的参数
                        // 也可以在其中设置 $this->handled 的值,中断后续事件的触发
                        call_user_func($handler[0], $event);
                        // 当某个 handled 被设置为 true 时,执行到这个事件的时候,会停止,并忽略剩下的事件
                        if ($event->handled) {
                            return;
                        }
                    }
                }
            } while (($class = get_parent_class($class)) !== false);
        }
    }
    
    
    Component.php少分析了几个方法,现在添加上去!
    
        /**
         * Detaches an existing event handler from this component.
         * 在该组件中,将现有的事件处理,
         * This method is the opposite of [[on()]].
         * 这种方法与on[]方法相反。
         * @param string $name event name
         * @param callable $handler the event handler to be removed.
         * If it is null, all handlers attached to the named event will be removed.
         * @return boolean if a handler is found and detached
         * @see on()
         */
        public function off($name, $handler = null)
        {
            // 保证 behaviors 都加载进来了
            $this->ensureBehaviors();
            // 相应的事件不存在,就返回false
            if (empty($this->_events[$name])) {
                return false;
            }
            // 没有handler,就意味着要全部去掉
            if ($handler === null) {
                unset($this->_events[$name]);
                return true;
            } else {
                $removed = false;//如果$handler不为空。
                foreach ($this->_events[$name] as $i => $event) {
                    // $event[0]是handler,$event[1]是数据
                    if ($event[0] === $handler) {
                        unset($this->_events[$name][$i]);
                        $removed = true;
                    }
                }
                if ($removed) {
                    // 如果有移出事件的handler,就需要重新构建以下索引,否则会出现index为1,3,4的情况
                    $this->_events[$name] = array_values($this->_events[$name]);
                }
                return $removed;
            }
        }
    
            /**
         * Triggers an event.
         * 触发一个事件。
         * This method represents the happening of an event. It invokes
         * all attached handlers for the event including class-level handlers.
         * 这种方法代表事件的发生。它调用的事件包括类级别的处理程序所包含的所有附加处理程序。
         * @param string $name the event name
         * @param Event $event the event parameter. If not set, a default [[Event]] object will be created.
         */
        public function trigger($name, Event $event = null)
        {
            // 保证 behaviors 都加载进来了
            $this->ensureBehaviors();
            if (!empty($this->_events[$name])) {
                // 构建Event对象,为传入到handler函数中做准备
                if ($event === null) {
                    $event = new Event;
                }
                if ($event->sender === null) { //如果$event->sender值为null的话,把$this赋值给它。
                    $event->sender = $this;
                }
                $event->handled = false; //使handled值为空
                $event->name = $name;
                foreach ($this->_events[$name] as $handler) {
                    // 给event的data属性赋值
                    $event->data = $handler[1];
                    // handler的函数中传入了一个Event对象
                    call_user_func($handler[0], $event);
                    // stop further handling if the event is handled
                    // 事件是否被handle,当handled被设置为true时,执行到这个event的时候,会停止,并忽略剩下的event
                    if ($event->handled) {
                        return;
                    }
                }
            }
            // invoke class-level attached handlers
            // 触发class级别的handler
            Event::trigger($this, $name, $event);
        }
  • 相关阅读:
    cms建站
    tab切换 原生js
    iOS下JS与原生OC互相调用(总结)
    HBuilder设置沉浸式状态栏显示效果
    JQuery 绑定select标签的onchange事件,弹出选择的值,并实现跳转、传参
    js调用app启动页
    CSS实现单行、多行文本溢出显示省略号(…)
    Java NIO系列教程(七) FileChannel
    Java NIO系列教程(六) Selector
    Java NIO系列教程(五) 通道之间的数据传输
  • 原文地址:https://www.cnblogs.com/taokai/p/5427660.html
Copyright © 2011-2022 走看看