You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					92 lines
				
				2.8 KiB
			
		
		
			
		
	
	
					92 lines
				
				2.8 KiB
			| 
											15 years ago
										 | <?php
 | ||
| 
											14 years ago
										 | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
| 
											15 years ago
										 | 
 | ||
|  | namespace yii\base;
 | ||
|  | 
 | ||
| 
											14 years ago
										 | /**
 | ||
|  |  * Behavior is the base class for all behavior classes.
 | ||
|  |  *
 | ||
| 
											14 years ago
										 |  * A behavior can be used to enhance the functionality of an existing component without modifying its code.
 | ||
| 
											14 years ago
										 |  * In particular, it can "inject" its own methods and properties into the component
 | ||
| 
											14 years ago
										 |  * and make them directly accessible via the component. It can also respond to the events triggered in the component
 | ||
|  |  * and thus intercept the normal code execution.
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											14 years ago
										 | class Behavior extends \yii\base\Object
 | ||
| 
											15 years ago
										 | {
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											14 years ago
										 | 	 * @var Component the owner of this behavior
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											14 years ago
										 | 	public $owner;
 | ||
| 
											15 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Declares event handlers for the [[owner]]'s events.
 | ||
|  | 	 *
 | ||
| 
											14 years ago
										 | 	 * Child classes may override this method to declare what PHP callbacks should
 | ||
|  | 	 * be attached to the events of the [[owner]] component.
 | ||
|  | 	 *
 | ||
|  | 	 * The callbacks will be attached to the [[owner]]'s events when the behavior is
 | ||
| 
											14 years ago
										 | 	 * attached to the owner; and they will be detached from the events when
 | ||
|  | 	 * the behavior is detached from the component.
 | ||
|  | 	 *
 | ||
| 
											14 years ago
										 | 	 * The callbacks can be any of the followings:
 | ||
|  | 	 *
 | ||
| 
											14 years ago
										 | 	 * - method in this behavior: `'handleClick'`, equivalent to `array($this, 'handleClick')`
 | ||
|  | 	 * - object method: `array($object, 'handleClick')`
 | ||
|  | 	 * - static method: `array('Page', 'handleClick')`
 | ||
| 
											14 years ago
										 | 	 * - anonymous function: `function($event) { ... }`
 | ||
|  | 	 *
 | ||
|  | 	 * The following is an example:
 | ||
| 
											14 years ago
										 | 	 *
 | ||
|  | 	 * ~~~
 | ||
|  | 	 * array(
 | ||
| 
											14 years ago
										 | 	 *	 'beforeValidate' => 'myBeforeValidate',
 | ||
|  | 	 *	 'afterValidate' => 'myAfterValidate',
 | ||
| 
											14 years ago
										 | 	 * )
 | ||
|  | 	 * ~~~
 | ||
| 
											14 years ago
										 | 	 *
 | ||
| 
											14 years ago
										 | 	 * @return array events (array keys) and the corresponding event handler methods (array values).
 | ||
| 
											15 years ago
										 | 	 */
 | ||
|  | 	public function events()
 | ||
|  | 	{
 | ||
|  | 		return array();
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Attaches the behavior object to the component.
 | ||
| 
											14 years ago
										 | 	 * The default implementation will set the [[owner]] property
 | ||
|  | 	 * and attach event handlers as declared in [[events]].
 | ||
| 
											15 years ago
										 | 	 * Make sure you call the parent implementation if you override this method.
 | ||
| 
											14 years ago
										 | 	 * @param Component $owner the component that this behavior is to be attached to.
 | ||
| 
											15 years ago
										 | 	 */
 | ||
|  | 	public function attach($owner)
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		$this->owner = $owner;
 | ||
| 
											14 years ago
										 | 		foreach ($this->events() as $event => $handler) {
 | ||
| 
											14 years ago
										 | 			$owner->on($event, is_string($handler) ? array($this, $handler) : $handler);
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											15 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Detaches the behavior object from the component.
 | ||
| 
											14 years ago
										 | 	 * The default implementation will unset the [[owner]] property
 | ||
|  | 	 * and detach event handlers declared in [[events]].
 | ||
| 
											15 years ago
										 | 	 * Make sure you call the parent implementation if you override this method.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function detach()
 | ||
| 
											15 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if ($this->owner) {
 | ||
|  | 			foreach ($this->events() as $event => $handler) {
 | ||
|  | 				$this->owner->off($event, is_string($handler) ? array($this, $handler) : $handler);
 | ||
|  | 			}
 | ||
|  | 			$this->owner = null;
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											15 years ago
										 | 	}
 | ||
|  | }
 |