Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
a421f9f1
Commit
a421f9f1
authored
Apr 05, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored component event.
parent
31777c92
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
107 additions
and
111 deletions
+107
-111
Component.md
docs/api/base/Component.md
+11
-13
Component.php
framework/base/Component.php
+69
-72
Event.php
framework/base/Event.php
+6
-3
ComponentTest.php
tests/unit/framework/base/ComponentTest.php
+21
-23
No files found.
docs/api/base/Component.md
View file @
a421f9f1
...
@@ -7,20 +7,20 @@ is triggered (i.e. comment will be added), our custom code will be executed.
...
@@ -7,20 +7,20 @@ is triggered (i.e. comment will be added), our custom code will be executed.
An event is identified by a name that should be unique within the class it is defined at. Event names are
*case-sensitive*
.
An event is identified by a name that should be unique within the class it is defined at. Event names are
*case-sensitive*
.
One or multiple PHP callbacks, called
*event handlers*
, c
ould
be attached to an event. You can call
[
[trigger()
]
] to
One or multiple PHP callbacks, called
*event handlers*
, c
an
be attached to an event. You can call
[
[trigger()
]
] to
raise an event. When an event is raised, the event handlers will be invoked automatically in the order they were
raise an event. When an event is raised, the event handlers will be invoked automatically in the order they were
attached.
attached.
To attach an event handler to an event, call
[
[on()
]
]:
To attach an event handler to an event, call
[
[on()
]
]:
~~~
~~~
$
comment->on('add
', function($event) {
$
post->on('update
', function($event) {
// send email notification
// send email notification
});
});
~~~
~~~
In the above,
we attach an anonymous function to the "add" event of the comment.
In the above,
an anonymous function is attached to the "update" event of the post. You may attach
Valid event handlers include
:
the following types of event handlers
:
-
anonymous function:
`function($event) { ... }`
-
anonymous function:
`function($event) { ... }`
-
object method:
`array($object, 'handleAdd')`
-
object method:
`array($object, 'handleAdd')`
...
@@ -35,8 +35,8 @@ function foo($event)
...
@@ -35,8 +35,8 @@ function foo($event)
where
`$event`
is an
[
[Event
]
] object which includes parameters associated with the event.
where
`$event`
is an
[
[Event
]
] object which includes parameters associated with the event.
You can also attach a
n event handler to an event when configuring a component with a configuration array. The syntax is
You can also attach a
handler to an event when configuring a component with a configuration array.
like the following:
The syntax is
like the following:
~~~
~~~
array(
array(
...
@@ -46,15 +46,13 @@ array(
...
@@ -46,15 +46,13 @@ array(
where
`on add`
stands for attaching an event to the
`add`
event.
where
`on add`
stands for attaching an event to the
`add`
event.
You can call
[
[getEventHandlers()
]
] to retrieve all event handlers that are attached to a specified event. Because this
Sometimes, you may want to associate extra data with an event handler when you attach it to an event
method returns a
[
[Vector
]
] object, we can manipulate this object to attach/detach event handlers, or adjust their
and then access it when the handler is invoked. You may do so by
relative orders.
~~~
~~~
$handlers = $comment->getEventHandlers('add');
$post->on('update', function($event) {
$handlers->insertAt(0, $callback); // attach a handler as the first one
// the data can be accessed via $event->data
$handlers[] = $callback; // attach a handler as the last one
}, $data);
unset($handlers[0]); // detach the first handler
~~~
~~~
...
...
framework/base/Component.php
View file @
a421f9f1
This diff is collapsed.
Click to expand it.
framework/base/Event.php
View file @
a421f9f1
...
@@ -15,12 +15,14 @@ namespace yii\base;
...
@@ -15,12 +15,14 @@ namespace yii\base;
* And the [[handled]] property indicates if the event is handled.
* And the [[handled]] property indicates if the event is handled.
* If an event handler sets [[handled]] to be true, the rest of the
* If an event handler sets [[handled]] to be true, the rest of the
* uninvoked handlers will no longer be called to handle the event.
* uninvoked handlers will no longer be called to handle the event.
* Additionally, an event may specify extra parameters via the [[data]] property.
*
* Additionally, when attaching an event handler, extra data may be passed
* and be available via the [[data]] property when the event handler is invoked.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
Event
extends
\yii\base\
Object
class
Event
extends
Object
{
{
/**
/**
* @var string the event name. This property is set by [[Component::trigger()]].
* @var string the event name. This property is set by [[Component::trigger()]].
...
@@ -39,7 +41,8 @@ class Event extends \yii\base\Object
...
@@ -39,7 +41,8 @@ class Event extends \yii\base\Object
*/
*/
public
$handled
=
false
;
public
$handled
=
false
;
/**
/**
* @var mixed extra custom data associated with the event.
* @var mixed the data that is passed to [[Component::on()]] when attaching an event handler.
* Note that this varies according to which event handler is currently executing.
*/
*/
public
$data
;
public
$data
;
}
}
tests/unit/framework/base/ComponentTest.php
View file @
a421f9f1
...
@@ -41,12 +41,12 @@ class ComponentTest extends TestCase
...
@@ -41,12 +41,12 @@ class ComponentTest extends TestCase
$component
->
attachBehavior
(
'a'
,
$behavior
);
$component
->
attachBehavior
(
'a'
,
$behavior
);
$this
->
assertSame
(
$behavior
,
$component
->
getBehavior
(
'a'
));
$this
->
assertSame
(
$behavior
,
$component
->
getBehavior
(
'a'
));
$component
->
on
(
'test'
,
'fake'
);
$component
->
on
(
'test'
,
'fake'
);
$this
->
assert
Equals
(
1
,
$component
->
getEventHandlers
(
'test'
)
->
count
);
$this
->
assert
True
(
$component
->
hasEventHandlers
(
'test'
)
);
$clone
=
clone
$component
;
$clone
=
clone
$component
;
$this
->
assertNotSame
(
$component
,
$clone
);
$this
->
assertNotSame
(
$component
,
$clone
);
$this
->
assertNull
(
$clone
->
getBehavior
(
'a'
));
$this
->
assertNull
(
$clone
->
getBehavior
(
'a'
));
$this
->
assert
Equals
(
0
,
$clone
->
getEventHandlers
(
'test'
)
->
count
);
$this
->
assert
False
(
$clone
->
hasEventHandlers
(
'test'
)
);
}
}
public
function
testHasProperty
()
public
function
testHasProperty
()
...
@@ -151,34 +151,32 @@ class ComponentTest extends TestCase
...
@@ -151,34 +151,32 @@ class ComponentTest extends TestCase
public
function
testOn
()
public
function
testOn
()
{
{
$this
->
assert
Equals
(
0
,
$this
->
component
->
getEventHandlers
(
'click'
)
->
getCount
(
));
$this
->
assert
False
(
$this
->
component
->
hasEventHandlers
(
'click'
));
$this
->
component
->
on
(
'click'
,
'foo'
);
$this
->
component
->
on
(
'click'
,
'foo'
);
$this
->
assertEquals
(
1
,
$this
->
component
->
getEventHandlers
(
'click'
)
->
getCount
());
$this
->
assertTrue
(
$this
->
component
->
hasEventHandlers
(
'click'
));
$this
->
component
->
on
(
'click'
,
'bar'
);
$this
->
assertEquals
(
2
,
$this
->
component
->
getEventHandlers
(
'click'
)
->
getCount
());
$p
=
'on click'
;
$this
->
component
->
$p
=
'foo2'
;
$this
->
assertEquals
(
3
,
$this
->
component
->
getEventHandlers
(
'click'
)
->
getCount
());
$this
->
component
->
getEventHandlers
(
'click'
)
->
add
(
'test'
);
$this
->
assertFalse
(
$this
->
component
->
hasEventHandlers
(
'click2'
));
$this
->
assertEquals
(
4
,
$this
->
component
->
getEventHandlers
(
'click'
)
->
getCount
());
$p
=
'on click2'
;
$this
->
component
->
$p
=
'foo2'
;
$this
->
assertTrue
(
$this
->
component
->
hasEventHandlers
(
'click2'
));
}
}
public
function
testOff
()
public
function
testOff
()
{
{
$this
->
assertFalse
(
$this
->
component
->
hasEventHandlers
(
'click'
));
$this
->
component
->
on
(
'click'
,
'foo'
);
$this
->
component
->
on
(
'click'
,
'foo'
);
$this
->
component
->
on
(
'click'
,
array
(
$this
->
component
,
'myEventHandler
'
));
$this
->
assertTrue
(
$this
->
component
->
hasEventHandlers
(
'click
'
));
$this
->
assertEquals
(
2
,
$this
->
component
->
getEventHandlers
(
'click'
)
->
getCount
()
);
$this
->
component
->
off
(
'click'
,
'foo'
);
$this
->
assertFalse
(
$this
->
component
->
hasEventHandlers
(
'click'
));
$result
=
$this
->
component
->
off
(
'click'
,
'foo'
);
$this
->
assertTrue
(
$result
);
$this
->
component
->
on
(
'click2'
,
'foo'
);
$this
->
assertEquals
(
1
,
$this
->
component
->
getEventHandlers
(
'click'
)
->
getCount
()
);
$this
->
component
->
on
(
'click2'
,
'foo2'
);
$
result
=
$this
->
component
->
off
(
'click'
,
'foo
'
);
$
this
->
component
->
on
(
'click2'
,
'foo3
'
);
$this
->
assert
False
(
$result
);
$this
->
assert
True
(
$this
->
component
->
hasEventHandlers
(
'click2'
)
);
$this
->
assertEquals
(
1
,
$this
->
component
->
getEventHandlers
(
'click'
)
->
getCount
()
);
$this
->
component
->
off
(
'click2'
,
'foo3'
);
$
result
=
$this
->
component
->
off
(
'click'
,
array
(
$this
->
component
,
'myEventHandler
'
));
$
this
->
assertTrue
(
$this
->
component
->
hasEventHandlers
(
'click2
'
));
$this
->
assertTrue
(
$result
);
$this
->
component
->
off
(
'click2'
);
$this
->
assert
Equals
(
0
,
$this
->
component
->
getEventHandlers
(
'click'
)
->
getCount
(
));
$this
->
assert
False
(
$this
->
component
->
hasEventHandlers
(
'click2'
));
}
}
public
function
testTrigger
()
public
function
testTrigger
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment