Commit 47fe25d4 by Qiang Xue

new event implementation.

parent e281b402
......@@ -19,7 +19,7 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Behavior extends Object
class Behavior extends \yii\base\Object
{
/**
* @var Component the owner component
......@@ -38,17 +38,17 @@ class Behavior extends Object
*
* The callbacks can be any of the followings:
*
* - method in this behavior: `'handleOnClick'`, equivalent to `array($this, 'handleOnClick')`
* - object method: `array($object, 'handleOnClick')`
* - static method: `array('Page', 'handleOnClick')`
* - method in this behavior: `'handleClick'`, equivalent to `array($this, 'handleClick')`
* - object method: `array($object, 'handleClick')`
* - static method: `array('Page', 'handleClick')`
* - anonymous function: `function($event) { ... }`
*
* The following is an example:
*
* ~~~
* array(
* 'onBeforeValidate' => 'myBeforeValidate',
* 'onAfterValidate' => 'myAfterValidate',
* 'beforeValidate' => 'myBeforeValidate',
* 'afterValidate' => 'myAfterValidate',
* )
* ~~~
*
......@@ -70,7 +70,7 @@ class Behavior extends Object
{
$this->_owner = $owner;
foreach ($this->events() as $event => $handler) {
$owner->attachEventHandler($event, is_string($handler) ? array($this, $handler) : $handler);
$owner->on($event, is_string($handler) ? array($this, $handler) : $handler);
}
}
......@@ -84,7 +84,7 @@ class Behavior extends Object
public function detach($owner)
{
foreach ($this->events() as $event => $handler) {
$owner->detachEventHandler($event, is_string($handler) ? array($this, $handler) : $handler);
$owner->off($event, is_string($handler) ? array($this, $handler) : $handler);
}
$this->_owner = null;
}
......
......@@ -24,9 +24,8 @@ class ModelBehavior extends Behavior
* Declares event handlers for owner's events.
* The default implementation returns the following event handlers:
*
* - `onAfterInit` event: [[afterInit]]
* - `onBeforeValidate` event: [[beforeValidate]]
* - `onAfterValidate` event: [[afterValidate]]
* - `beforeValidate` event
* - `afterValidate` event
*
* You may override these event handler methods to respond to the corresponding owner events.
* @return array events (array keys) and the corresponding event handler methods (array values).
......@@ -34,22 +33,12 @@ class ModelBehavior extends Behavior
public function events()
{
return array(
'onAfterInit' => 'afterInit',
'onBeforeValidate' => 'beforeValidate',
'onAfterValidate' => 'afterValidate',
'beforeValidate' => 'beforeValidate',
'afterValidate' => 'afterValidate',
);
}
/**
* Responds to [[Model::onAfterInit]] event.
* Override this method if you want to handle the corresponding event of the [[owner]].
* @param Event $event event parameter
*/
public function afterInit($event)
{
}
/**
* Responds to [[Model::onBeforeValidate]] event.
* Override this method if you want to handle the corresponding event of the [[owner]].
* You may set the [[ModelEvent::isValid|isValid]] property of the event parameter
......
......@@ -294,7 +294,7 @@ class Object
* ~~~
*
* @param array $config the object configuration (name-value pairs that will be used to initialize the object)
* @return Object the created object
* @return \yii\base\Object the created object
* @throws Exception if the configuration is invalid.
*/
public static function newInstance($config = array())
......@@ -326,7 +326,7 @@ class Object
$object->$name = $value;
}
if ($object instanceof \yii\base\Initable) {
if ($object instanceof Initable) {
$object->init();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment