Component.md 3.02 KB
Newer Older
Qiang Xue committed
1 2 3 4 5
Component provides the *event* and *behavior* features, in addition to the *property* feature which is implemented in
its parent class [[Object]].

Event is a way to "inject" custom code into existing code at certain places. For example, a comment object can trigger
an "add" event when the user adds a comment. We can write custom code and attach it to this event so that when the event
6
is triggered (i.e. comment will be added), our custom code will be executed.
Qiang Xue committed
7

8
An event is identified by a name that should be unique within the class it is defined at. Event names are *case-sensitive*.
Qiang Xue committed
9

10
One or multiple PHP callbacks, called *event handlers*, could be attached to an event. You can call [[trigger()]] to
11 12
raise an event. When an event is raised, the event handlers will be invoked automatically in the order they were
attached.
Qiang Xue committed
13

14
To attach an event handler to an event, call [[on()]]:
Qiang Xue committed
15 16 17 18 19 20 21

~~~
$comment->on('add', function($event) {
    // send email notification
});
~~~

22 23
In the above, we attach an anonymous function to the "add" event of the comment.
Valid event handlers include:
Qiang Xue committed
24 25 26

- anonymous function: `function($event) { ... }`
- object method: `array($object, 'handleAdd')`
27
- static class method: `array('Page', 'handleAdd')`
Qiang Xue committed
28 29 30 31 32 33 34 35 36 37
- global function: `'handleAdd'`

The signature of an event handler should be like the following:

~~~
function foo($event)
~~~

where `$event` is an [[Event]] object which includes parameters associated with the event.

38
You can also attach an event handler to an event when configuring a component with a configuration array. The syntax is
Qiang Xue committed
39 40 41 42 43 44 45 46 47 48
like the following:

~~~
array(
    'on add' => function($event) { ... }
)
~~~

where `on add` stands for attaching an event to the `add` event.

49
You can call [[getEventHandlers()]] to retrieve all event handlers that are attached to a specified event. Because this
Qiang Xue committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
method returns a [[Vector]] object, we can manipulate this object to attach/detach event handlers, or adjust their
relative orders.

~~~
$handlers = $comment->getEventHandlers('add');
$handlers->insertAt(0, $callback); // attach a handler as the first one
$handlers[] = $callback;           // attach a handler as the last one
unset($handlers[0]);               // detach the first handler
~~~


A behavior is an instance of [[Behavior]] or its child class. A component can be attached with one or multiple
behaviors. When a behavior is attached to a component, its public properties and methods can be accessed via the
component directly, as if the component owns those properties and methods.

To attach a behavior to a component, declare it in [[behaviors()]], or explicitly call [[attachBehavior]]. Behaviors
declared in [[behaviors()]] are automatically attached to the corresponding component.

One can also attach a behavior to a component when configuring it with a configuration array. The syntax is like the
following:

~~~
array(
    'as tree' => array(
        'class' => 'Tree',
    ),
)
~~~

where `as tree` stands for attaching a behavior named `tree`, and the array will be passed to [[\Yii::createObject()]]
to create the behavior object.