5
<title>Example: Custom Event Bubbling and Behaviors</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Custom Event Bubbling and Behaviors</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><style type="text/css">
27
border: 1px dotted #999;
28
background-color: #FFF;
40
The Custom Event framework is one of the principle communication
41
mechanisms in YUI. An object can be augmented with
42
<code>EventTarget</code>, enabling it to be both a host and a target
43
for custom events. Custom events fire from their host and optionally
44
bubble up to one or more targets. This allows you to make the
45
interesting moments of your applications broadly available within a
46
module, within a set of modules, or throughout a complex interface
47
populated with rich elements.
51
In this example, a simple custom event is illustrated:
52
<code>testEvent</code>. This custom event is hosted on a Publisher
53
object and bubbles up to a BubbleTarget object.
56
<img src="../assets/event-custom/ce-example.gif"
57
alt="An illustration of the relationship between the custom event, its host, and its Bubble Target.">
60
Like DOM events, custom event bubbling can be stopped with
61
<code>e.stopPropagation()</code> and default behavior can be suppressed with
62
<code>e.preventDefault()</code>.
66
<div class="example yui3-skin-sam">
68
<button id="fire" value="Fire">Fire testEvent</button>
72
<input type="checkbox" id="stopPropagation">
73
<label for="stopPropagation">
74
Stop Propagation (testEvent won't bubble to the BubbleTarget.)
78
<input type="checkbox" id="preventDefault">
79
<label for="preventDefault">
80
Prevent Default (testEvent's <code>defaultFn</code> won't fire.)
85
<li>Custom Event log messages will appear here.</li>
88
// Bubbling events are added by the event-custom module.
89
YUI().use('event-custom', 'node', function (Y) {
91
var logger = Y.one("#log");
92
stopCheckbox = Y.one("#stopPropagation"),
93
preventCheckbox = Y.one("#preventDefault");
95
// We'll create two classes, one to fire the event, and another to be a
96
// bubble target for the other. All events from the Publisher class can
97
// then be subscribed from either the Publisher instance or the BubbleTarget
98
// instance that it's related to.
99
function BubbleTarget() {
100
Y.log("BubbleTarget constructor executed.");
103
function Publisher(bubbleTo) {
104
Y.log("Publisher constructor executed.");
106
this.init(bubbleTo); // see class prototype below
109
// Publishers need to add the provided target to their bubble chain with
110
// <code>addTarget</code>. We'll do this, and publish an event, in an <code>init</code> method
111
Publisher.prototype = {
112
init: function (bubbleTo) {
114
// <code>addTarget</code> is the EventTarget method to register new bubble
115
// targets for this instance
116
this.addTarget(bubbleTo);
118
// It's only necessary to publish events with special configuration,
119
// such as default, stop, or prevent behaviors. You can always
120
// fire any event name you wish, published or unpublished.
121
this.publish("testEvent", {
122
// Pass an event facade to subscribers so they can call
123
// e.preventDefault() and other methods.
126
// An event's default behavior is defined in defaultFn. This
127
// will execute unless a subscriber calls <code>e.preventDefault()</code>
128
defaultFn: function () {
129
Y.log("defaultFn: testEvent's defaultFn executed.");
132
// You can react to subscribers preventing default behavior as
133
// well, by defining a preventedFn.
134
preventedFn: function () {
135
Y.log("preventedFn: A subscriber to testEvent called preventDefault().");
138
// If a subscriber calls <code>e.stopPropagation()</code>, the event won't
139
// bubble any further, and the stoppedFn will be called if one
141
stoppedFn: function () {
142
Y.log("stoppedFn: A subscriber to testEvent called stopPropagation().");
149
// To fire events or be a bubble target, augment a class with EventTarget
150
Y.augment(Publisher, Y.EventTarget);
151
Y.augment(BubbleTarget, Y.EventTarget);
156
var bubbleTarget = new BubbleTarget();
158
// You can subscribe to the "testEvent" from the BubbleTarget, even before
159
// a Publisher is created
160
bubbleTarget.subscribe("testEvent", function (e) {
161
Y.log("testEvent fired on the BubbleTarget object.");
164
// Create a Publisher instance, and link it to our BubbleTarget
165
var publisher = new Publisher(bubbleTarget);
167
// We can also subscribe to the testEvent on the Publisher instance.
168
publisher.on("testEvent", function (e) {
169
Y.log("testEvent subscriber fired on the publisher object.");
171
if (stopCheckbox.get("checked")) {
175
if (preventCheckbox.get("checked")) {
181
// Wire up the example button to fire the event from our publisher
182
Y.one("#fire").on("click", function (e) {
183
logger.empty(); // clear out the logger:
185
publisher.fire("testEvent");
188
// A little supporting magic to output Y.log() statements to the screen
189
Y.on("yui:log", function (e) {
190
logger.append("<li>" + e.msg + "</li>");
201
The full source code for this example follows. Read through the comments
202
and code to get an understanding of how you can make use of custom events
203
in your own application development.
206
<pre class="code prettyprint">// Bubbling events are added by the event-custom module.
207
YUI().use('event-custom', 'node', function (Y) {
209
var logger = Y.one("#log");
210
stopCheckbox = Y.one("#stopPropagation"),
211
preventCheckbox = Y.one("#preventDefault");
213
// We'll create two classes, one to fire the event, and another to be a
214
// bubble target for the other. All events from the Publisher class can
215
// then be subscribed from either the Publisher instance or the BubbleTarget
216
// instance that it's related to.
217
function BubbleTarget() {
218
Y.log("BubbleTarget constructor executed.");
221
function Publisher(bubbleTo) {
222
Y.log("Publisher constructor executed.");
224
this.init(bubbleTo); // see class prototype below
227
// Publishers need to add the provided target to their bubble chain with
228
// `addTarget`. We'll do this, and publish an event, in an `init` method
229
Publisher.prototype = {
230
init: function (bubbleTo) {
232
// `addTarget` is the EventTarget method to register new bubble
233
// targets for this instance
234
this.addTarget(bubbleTo);
236
// It's only necessary to publish events with special configuration,
237
// such as default, stop, or prevent behaviors. You can always
238
// fire any event name you wish, published or unpublished.
239
this.publish("testEvent", {
240
// Pass an event facade to subscribers so they can call
241
// e.preventDefault() and other methods.
244
// An event's default behavior is defined in defaultFn. This
245
// will execute unless a subscriber calls `e.preventDefault()`
246
defaultFn: function () {
247
Y.log("defaultFn: testEvent's defaultFn executed.");
250
// You can react to subscribers preventing default behavior as
251
// well, by defining a preventedFn.
252
preventedFn: function () {
253
Y.log("preventedFn: A subscriber to testEvent called preventDefault().");
256
// If a subscriber calls `e.stopPropagation()`, the event won't
257
// bubble any further, and the stoppedFn will be called if one
258
// is defined.
259
stoppedFn: function () {
260
Y.log("stoppedFn: A subscriber to testEvent called stopPropagation().");
267
// To fire events or be a bubble target, augment a class with EventTarget
268
Y.augment(Publisher, Y.EventTarget);
269
Y.augment(BubbleTarget, Y.EventTarget);
272
// SEE IT IN ACTION
274
var bubbleTarget = new BubbleTarget();
276
// You can subscribe to the "testEvent" from the BubbleTarget, even before
277
// a Publisher is created
278
bubbleTarget.subscribe("testEvent", function (e) {
279
Y.log("testEvent fired on the BubbleTarget object.");
282
// Create a Publisher instance, and link it to our BubbleTarget
283
var publisher = new Publisher(bubbleTarget);
285
// We can also subscribe to the testEvent on the Publisher instance.
286
publisher.on("testEvent", function (e) {
287
Y.log("testEvent subscriber fired on the publisher object.");
289
if (stopCheckbox.get("checked")) {
293
if (preventCheckbox.get("checked")) {
299
// Wire up the example button to fire the event from our publisher
300
Y.one("#fire").on("click", function (e) {
301
logger.empty(); // clear out the logger:
303
publisher.fire("testEvent");
306
// A little supporting magic to output Y.log() statements to the screen
307
Y.on("yui:log", function (e) {
308
logger.append("<li>" + e.msg + "</li>");
318
<div class="yui3-u-1-4">
319
<div class="sidebar">
323
<div class="sidebox">
325
<h2 class="no-toc">Examples</h2>
329
<ul class="examples">
332
<li data-description="Publish an event with a default behavior, as well as behaviors for reacting to preventing the default or stopping bubbling.">
333
<a href="flow-example.html">Custom Event Bubbling and Behaviors</a>
348
<script src="../assets/vendor/prettify/prettify-min.js"></script>
349
<script>prettyPrint();</script>