Commit 47fe25d4 by Qiang Xue

new event implementation.

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