|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>EventTarget</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>EventTarget</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><link type="text/css" rel="stylesheet" href="../../build/cssbutton/cssbutton-min.css"> |
|
|
31 |
<div class="intro"> |
|
|
32 |
<p> |
|
|
33 |
The YUI Custom Event System enables you to define and use events beyond |
|
|
34 |
those available in the DOM — events that are specific to and of |
|
|
35 |
interest in your own application. Custom Events are designed to work |
|
|
36 |
much like DOM events. They can bubble, pass event facades, have their |
|
|
37 |
propagation and default behaviors suppressed, etc. |
|
|
38 |
</p> |
|
|
39 |
|
|
|
40 |
<p> |
|
|
41 |
The APIs for working with custom events are provided by the |
|
|
42 |
<code>EventTarget</code> class. All other infrastructure classes extend |
|
|
43 |
<code>EventTarget</code>, but if you just need the custom event APIs, you can |
|
|
44 |
<code>extend</code> or <code>augment</code> your classes with <code>EventTarget</code> directly. |
|
|
45 |
</p> |
|
|
46 |
|
|
|
47 |
<p class="deprecated"><strong>DEPRECATION NOTE:</strong> The <code>subscribers</code> and <code>afters</code> properties which |
|
|
48 |
used to sit on <code>CustomEvent</code> object instances have been deprecated and |
|
|
49 |
removed for performance reasons as of the 3.7.0 release. |
|
|
50 |
</p> |
|
|
51 |
|
|
|
52 |
<p>If you're referring to the <code>subscribers</code> or <code>afters</code> properties directly just |
|
|
53 |
to access the set of subscribers, consider switching to the public <code>getSubs()</code> |
|
|
54 |
method instead which hides you from the implementation details.</p> |
|
|
55 |
|
|
|
56 |
<p>If you have a use case which requires you to access the above properties |
|
|
57 |
directly you can set <code>Y.CustomEvent.keepDeprecatedSubs</code> to true, to restore |
|
|
58 |
them, but you will incur a performance hit if you enable this flag. |
|
|
59 |
</p> |
|
|
60 |
|
|
|
61 |
<!--p> |
|
|
62 |
Bundled with <code>EventTarget</code> are <a |
|
|
63 |
href="http://en.wikipedia.org/wiki/Aspect_oriented_programming">Aspect |
|
|
64 |
Oriented Programming</a> methods that allow you to subscribe to the |
|
|
65 |
execution of object methods, and |
|
|
66 |
their own. |
|
|
67 |
</p--> |
|
|
68 |
</div> |
|
|
69 |
|
|
|
70 |
<!-- insert Events Evolved video here --> |
|
|
71 |
|
|
|
72 |
<h2 id="getting-started">Getting Started</h2> |
|
|
73 |
|
|
|
74 |
<p> |
|
|
75 |
To include the source files for EventTarget and its dependencies, first load |
|
|
76 |
the YUI seed file if you haven't already loaded it. |
|
|
77 |
</p> |
|
|
78 |
|
|
|
79 |
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.10.3/build/yui/yui-min.js"></script></pre> |
|
|
80 |
|
|
|
81 |
|
|
|
82 |
<p> |
|
|
83 |
Next, create a new YUI instance for your application and populate it with the |
|
|
84 |
modules you need by specifying them as arguments to the <code>YUI().use()</code> method. |
|
|
85 |
YUI will automatically load any dependencies required by the modules you |
|
|
86 |
specify. |
|
|
87 |
</p> |
|
|
88 |
|
|
|
89 |
<pre class="code prettyprint"><script> |
|
|
90 |
// Create a new YUI instance and populate it with the required modules. |
|
|
91 |
YUI().use('event-custom', function (Y) { |
|
|
92 |
// EventTarget is available and ready for use. Add implementation |
|
|
93 |
// code here. |
|
|
94 |
}); |
|
|
95 |
</script></pre> |
|
|
96 |
|
|
|
97 |
|
|
|
98 |
<p> |
|
|
99 |
For more information on creating YUI instances and on the |
|
|
100 |
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the |
|
|
101 |
documentation for the <a href="../yui/index.html">YUI Global Object</a>. |
|
|
102 |
</p> |
|
|
103 |
|
|
|
104 |
|
|
|
105 |
<h2 id="video-overview">Video Overview</h2> |
|
|
106 |
|
|
|
107 |
<iframe width="640" height="360" src="http://www.youtube.com/embed/s_7VjN3qxe8" frameborder="0" allowfullscreen></iframe> |
|
|
108 |
|
|
|
109 |
<p> |
|
|
110 |
This video from YUIConf 2009 gives a good overview of the YUI event system |
|
|
111 |
API. The content covers DOM and custom events. Note: the <a |
|
|
112 |
href="../event/index.html#synthetic-events">synthetic event</a> system was |
|
|
113 |
updated since this video. |
|
|
114 |
</p> |
|
|
115 |
|
|
|
116 |
<h2 id="the-basics">The Basics</h2> |
|
|
117 |
|
|
|
118 |
<p> |
|
|
119 |
You can get started using custom events and the <code>EventTarget</code> API without |
|
|
120 |
creating your own class. The YUI instance (typically <code>Y</code>) is an |
|
|
121 |
<code>EventTarget</code>, as is pretty much every other class in YUI. We'll go over |
|
|
122 |
the basics using <code>Y</code>, then move into creating your own <code>EventTarget</code>s. |
|
|
123 |
</p> |
|
|
124 |
|
|
|
125 |
<p> |
|
|
126 |
If you've looked over the <a href="../event/index.html#the-basics">DOM |
|
|
127 |
Event system docs</a>, this should look very familiar. That's because |
|
|
128 |
<code>Node</code>s are also <code>EventTarget</code>s. |
|
|
129 |
</p> |
|
|
130 |
|
|
|
131 |
<h3 id="subscribing-to-events">Subscribing to Events</h3> |
|
|
132 |
|
|
|
133 |
<pre class="code prettyprint">// Custom events can have any name you want |
|
|
134 |
Y.on('anyOldNameYouWant', function () { |
|
|
135 |
alert("Looky there!"); |
|
|
136 |
}); |
|
|
137 |
|
|
|
138 |
// Group subscriptions by passing an object or array to on() |
|
|
139 |
Y.on({ |
|
|
140 |
somethingImportant: updateCalendar, |
|
|
141 |
birthday : eatCake, |
|
|
142 |
weekendEnd : backToTheGrindstone |
|
|
143 |
}); |
|
|
144 |
|
|
|
145 |
// Some events have prefixes |
|
|
146 |
Y.once("fuji:available", climb); |
|
|
147 |
|
|
|
148 |
// Custom events have distinct "after" moments |
|
|
149 |
Y.after("spa-category|pedicure", gelatoTime);</pre> |
|
|
150 |
|
|
|
151 |
|
|
|
152 |
<p> |
|
|
153 |
All <code>EventTarget</code>s host methods |
|
|
154 |
<a href="http://yuilibrary.com/yui/docs/api/classes/EventTarget.html#method_on"><code>on</code></a>, |
|
|
155 |
<a href="http://yuilibrary.com/yui/docs/api/classes/EventTarget.html#method_once"><code>once</code></a>, |
|
|
156 |
<a href="http://yuilibrary.com/yui/docs/api/classes/EventTarget.html#method_after"><code>after</code></a>, and |
|
|
157 |
<a href="http://yuilibrary.com/yui/docs/api/classes/EventTarget.html#method_onceAfter"><code>onceAfter</code></a>. |
|
|
158 |
Both <code>once</code> and <code>onceAfter</code> will automatically detach the subscription |
|
|
159 |
after the callback is executed the first time. All subscription methods |
|
|
160 |
return a subscription object called an |
|
|
161 |
<a href="http://yuilibrary.com/yui/docs/api/classes/EventHandle.html">EventHandle</a>. The |
|
|
162 |
distinction between <code>on</code> and <code>after</code> is discussed in the |
|
|
163 |
<a href="#after">"after" phase</a> section below. |
|
|
164 |
</p> |
|
|
165 |
|
|
|
166 |
<h3 id="fire">Firing Events</h3> |
|
|
167 |
|
|
|
168 |
<pre class="code prettyprint">// All subscribers to the myapp:ready event will be executed |
|
|
169 |
Y.fire('myapp:ready'); |
|
|
170 |
|
|
|
171 |
// Pass along relevant data to the callbacks as arguments |
|
|
172 |
Y.fire('birthday', { |
|
|
173 |
name: 'Walt Disney', |
|
|
174 |
birthdate: new Date(1901, 11, 5) |
|
|
175 |
});</pre> |
|
|
176 |
|
|
|
177 |
|
|
|
178 |
<p id="event-data-object"> |
|
|
179 |
Notify event subscribers by calling <code>fire( eventName )</code>, passing any |
|
|
180 |
extra data about the event as additional arguments. Though <code>fire</code> |
|
|
181 |
accepts any number of arguments, it is preferable to send all event data |
|
|
182 |
in an object passed as the second argument. Doing so avoids locking your |
|
|
183 |
code into a specific <code>fire</code> and callback signature. |
|
|
184 |
</p> |
|
|
185 |
|
|
|
186 |
<pre class="code prettyprint">// Subscription callbacks receive fire() arguments |
|
|
187 |
Y.on('birthday', function (name, birthdate) { |
|
|
188 |
var age = new Date().getFullYear() - birthdate.getFullYear(); |
|
|
189 |
alert('Happy ' + age + ', ' + name + '!'); |
|
|
190 |
}); |
|
|
191 |
|
|
|
192 |
// Possible, but not recommended |
|
|
193 |
Y.fire('birthday', 'A. A. Milne', new Date(1882, 0, 18)); |
|
|
194 |
|
|
|
195 |
// Instead, try to always pass only one object with all data |
|
|
196 |
Y.on('birthday', function (e) { |
|
|
197 |
var age = new Date().getFullYear() - e.birthdate.getFullYear(); |
|
|
198 |
alert('Happy ' + age + ', ' + e.name + '!'); |
|
|
199 |
}); |
|
|
200 |
|
|
|
201 |
Y.fire('birthday', { |
|
|
202 |
name: '"Uncle" Walt Whitman', |
|
|
203 |
birthdate: new Date(1819, 4, 31) |
|
|
204 |
});</pre> |
|
|
205 |
|
|
|
206 |
|
|
|
207 |
<p> |
|
|
208 |
In the world of DOM events, the <code>fire</code> step is something the browser is |
|
|
209 |
responsible for. A typical model involves the browser receiving keyboard |
|
|
210 |
input from the user and firing <code>keydown</code>, <code>keyup</code>, and <code>keypress</code> events. |
|
|
211 |
Custom events put your code in the position of dispatching events in |
|
|
212 |
response to criteria that are relavant to your objects or application. |
|
|
213 |
</p> |
|
|
214 |
|
|
|
215 |
<h3 id="callback-arguments-and-event-facades">Callback arguments and event facades</h3> |
|
|
216 |
|
|
|
217 |
<pre class="code prettyprint">// Simple notification events don't send event objects, only fire() data |
|
|
218 |
Y.on('talkie', function (data) { |
|
|
219 |
alert('(' + data.time + ') Walkie ' + data.message); |
|
|
220 |
// data.preventDefault is not defined. data is just a plain object |
|
|
221 |
}); |
|
|
222 |
|
|
|
223 |
Y.fire('talkie', { |
|
|
224 |
message: 'roger, over.', |
|
|
225 |
time: new Date() |
|
|
226 |
}); |
|
|
227 |
|
|
|
228 |
// Events configured to emitFacade will send an event object, merged with |
|
|
229 |
// fire() data |
|
|
230 |
Y.publish('bill:due', { |
|
|
231 |
emitFacade: true, |
|
|
232 |
defaultFn : payUp |
|
|
233 |
}); |
|
|
234 |
|
|
|
235 |
Y.on('bill:due', function (e) { |
|
|
236 |
// Event facades have standard properties and methods as well as properties |
|
|
237 |
// from payload data passed to fire() |
|
|
238 |
if (e.payee === 'Rich Uncle Sal') { |
|
|
239 |
e.preventDefault(); // the `payUp` method won't be called (Sal can wait) |
|
|
240 |
} |
|
|
241 |
}); |
|
|
242 |
|
|
|
243 |
// Objects passed as the second argument to fire() for facade events will have |
|
|
244 |
// their properties merged onto the facade received by the callback. |
|
|
245 |
Y.fire('bill:due', { |
|
|
246 |
payee: 'Great Aunt Myra', |
|
|
247 |
amount: 20 |
|
|
248 |
});</pre> |
|
|
249 |
|
|
|
250 |
|
|
|
251 |
<p> |
|
|
252 |
Custom event callbacks are <em>usually, but not always</em> passed an |
|
|
253 |
<a href="http://yuilibrary.com/yui/docs/api/classes/EventFacade.html">EventFacade</a> as their |
|
|
254 |
first argument. Custom events can be configured to send event facades or |
|
|
255 |
only the data they were fired with. <a href="#event-data-object">Always |
|
|
256 |
passing event data in an object</a> as the second argument to <code>fire</code> allows |
|
|
257 |
you to write all your callbacks to expect event data as a single first |
|
|
258 |
argument, whether it's an <code>EventFacade</code> or just a plain object. The |
|
|
259 |
<code>emitFacade</code> and <code>defaultFn</code> configurations are detailed below, in |
|
|
260 |
<a href="#publishing-events">Publishing Events</a>. |
|
|
261 |
</p> |
|
|
262 |
|
|
|
263 |
<h3 id="detaching-event-subscriptions">Detaching Event Subscriptions</h3> |
|
|
264 |
|
|
|
265 |
<pre class="code prettyprint">// Subscription methods return a subscription handle... |
|
|
266 |
var subscription = Y.on('myapp:ready', initComponents); |
|
|
267 |
|
|
|
268 |
// ...with a detach method |
|
|
269 |
subscription.detach(); |
|
|
270 |
|
|
|
271 |
// Or detach by signature |
|
|
272 |
Y.detach('myapp:ready', initComponents); |
|
|
273 |
|
|
|
274 |
// Or by subscription category |
|
|
275 |
Y.on('spa-category|pedicure', gelatoTime); |
|
|
276 |
|
|
|
277 |
// Detach subscriptions to all events in the spa-category subscription group |
|
|
278 |
Y.detach('spa-category|*');</pre> |
|
|
279 |
|
|
|
280 |
|
|
|
281 |
<p> |
|
|
282 |
The preferred method of detaching subscriptions is to use the |
|
|
283 |
<a href="http://yuilibrary.com/yui/docs/api/classes/EventHandle.html">EventHandle</a> that is |
|
|
284 |
returned from the subscription methods. Alternately you can use the |
|
|
285 |
<a href="http://yuilibrary.com/yui/docs/api/classes/EventTarget.html#method_detach"><code>detach</code> or |
|
|
286 |
<code>detachAll</code> methods</a> which work as described in the |
|
|
287 |
<a href="../event/index.html#detach-methods">Event user guide</a>. |
|
|
288 |
</p> |
|
|
289 |
|
|
|
290 |
<h3 id="extend-event-target">Extending EventTarget</h3> |
|
|
291 |
|
|
|
292 |
<p>Add the <code>EventTarget</code> APIs onto any class using <code>Y.augment()</code>.</p> |
|
|
293 |
|
|
|
294 |
<pre class="code prettyprint">function MyClass() { |
|
|
295 |
/* insert constructor logic here */ |
|
|
296 |
} |
|
|
297 |
|
|
|
298 |
MyClass.prototype = { |
|
|
299 |
add: function (item) { |
|
|
300 |
// You can assume the APIs are available from your class instances |
|
|
301 |
this.fire("addItem", { item: item }); |
|
|
302 |
}, |
|
|
303 |
... |
|
|
304 |
}; |
|
|
305 |
|
|
|
306 |
// Make MyClass an EventTarget |
|
|
307 |
Y.augment(MyClass, Y.EventTarget); |
|
|
308 |
|
|
|
309 |
var instance = new MyClass(); |
|
|
310 |
instance.on('addItem', function (e) { |
|
|
311 |
alert("Yay, I'm adding " + e.item); |
|
|
312 |
}); |
|
|
313 |
|
|
|
314 |
instance.add('a new item'); // ==> "Yay, I'm adding a new item"</pre> |
|
|
315 |
|
|
|
316 |
|
|
|
317 |
<p> |
|
|
318 |
<code>Y.augment</code> works like a lazy <code>extend</code> or a mixin. It adds the APIs to the |
|
|
319 |
host class, but on the first call to any of the methods, it calls the |
|
|
320 |
<code>EventTarget</code> constructor on the instance to make sure the necessary |
|
|
321 |
internal objects are ready for use. If your class extends another, |
|
|
322 |
augmenting it won't interfere with that inheritance hierarchy. |
|
|
323 |
</p> |
|
|
324 |
|
|
|
325 |
<p> |
|
|
326 |
<code>EventTarget</code>s can be set up with a number of default configurations for |
|
|
327 |
the events they <code>fire</code>. Pass the defaults as the fourth argument to |
|
|
328 |
<code>Y.augment</code>. |
|
|
329 |
</p> |
|
|
330 |
|
|
|
331 |
<pre class="code prettyprint">// Make all events fired from MyClass instances send an event facade |
|
|
332 |
Y.augment(MyClass, Y.EventTarget, true, null, { |
|
|
333 |
emitFacade: true |
|
|
334 |
});</pre> |
|
|
335 |
|
|
|
336 |
|
|
|
337 |
<h2 id="publishing-events">Publishing Events</h2> |
|
|
338 |
|
|
|
339 |
<p> |
|
|
340 |
Some custom event <a href="#configs">configurations can be defaulted</a> |
|
|
341 |
from class configuration, but others need to be specified on a per-event |
|
|
342 |
basis. Use the <code>publish</code> method to do this. |
|
|
343 |
</p> |
|
|
344 |
|
|
|
345 |
<pre class="code prettyprint">// publish takes an event name and a configuration object |
|
|
346 |
Y.publish('somethingSpecial', { |
|
|
347 |
emitFacade: true, |
|
|
348 |
broadcast: 2, |
|
|
349 |
defaultFn: clapClapHallelujah, |
|
|
350 |
fireOnce: true |
|
|
351 |
});</pre> |
|
|
352 |
|
|
|
353 |
|
|
|
354 |
<h3 id="facade">Event Facades</h3> |
|
|
355 |
|
|
|
356 |
<p> |
|
|
357 |
The most common configuration for custom events is <code>emitFacade</code>. This is |
|
|
358 |
because with the event facades comes a lot of additional functionality, |
|
|
359 |
such as <a href="#defaultFn">preventable default behaviors</a> and <a |
|
|
360 |
href="#bubbling">bubbling</a>. |
|
|
361 |
</p> |
|
|
362 |
|
|
|
363 |
<pre class="code prettyprint">function Recipe() { |
|
|
364 |
// publishing events is typically done at instantiation |
|
|
365 |
this.publish('add', { |
|
|
366 |
emitFacade: true, |
|
|
367 |
defaultFn: this._defAddFn |
|
|
368 |
}); |
|
|
369 |
}</pre> |
|
|
370 |
|
|
|
371 |
|
|
|
372 |
<p> |
|
|
373 |
Event facades mirror the event objects |
|
|
374 |
<a href="../event/index.html#facade-properties">you're familiar with from |
|
|
375 |
the DOM</a>. They have properties like <code>e.type</code> and <code>e.target</code> and |
|
|
376 |
the same methods, allowing you to call <code>e.preventDefault()</code> to disable |
|
|
377 |
default behavior you've configured for the event or <code>e.stopPropagation()</code> |
|
|
378 |
to stop the event from bubbling. |
|
|
379 |
</p> |
|
|
380 |
|
|
|
381 |
<pre class="code prettyprint">var gruel = new Recipe(); |
|
|
382 |
|
|
|
383 |
gruel.on('add', function (e) { |
|
|
384 |
if (e.item === "brussel sprouts") { |
|
|
385 |
// call e.preventDefault() just as you would for DOM events |
|
|
386 |
e.preventDefault(); // brussel sprouts? eww! |
|
|
387 |
} |
|
|
388 |
});</pre> |
|
|
389 |
|
|
|
390 |
|
|
|
391 |
<p> |
|
|
392 |
<code>emitFacade</code> is typically passed as a default configuration to <code>Y.augment</code>. |
|
|
393 |
All other YUI infrastructure classes extend <code>EventTarget</code> and set |
|
|
394 |
<code>emitFacade</code> to <code>true</code> for you. |
|
|
395 |
</p> |
|
|
396 |
|
|
|
397 |
<pre class="code prettyprint">Y.extend(MyClass, Y.Base, { |
|
|
398 |
add: function (item) { |
|
|
399 |
// This will fire with an event facade because Y.Base sets emitFacade to true |
|
|
400 |
this.fire('addItem', { item: item }); |
|
|
401 |
}, |
|
|
402 |
... |
|
|
403 |
});</pre> |
|
|
404 |
|
|
|
405 |
|
|
|
406 |
<h3 id="once"><code>fireOnce</code> Events</h3> |
|
|
407 |
|
|
|
408 |
<p> |
|
|
409 |
Important, typically system-level or lifecycle related events can be |
|
|
410 |
configured as <code>fireOnce</code>. These events mimic things like <code>window.onload</code> |
|
|
411 |
or the <code>domready</code> event. |
|
|
412 |
</p> |
|
|
413 |
|
|
|
414 |
<pre class="code prettyprint">Widget.prototype.render = function (where) { |
|
|
415 |
... |
|
|
416 |
|
|
|
417 |
// Widget rendering only happens once |
|
|
418 |
this.publish('render', { |
|
|
419 |
defaultFn: this._defRenderFn, |
|
|
420 |
fireOnce: true, |
|
|
421 |
... |
|
|
422 |
}); |
|
|
423 |
|
|
|
424 |
this.fire('render', ...); |
|
|
425 |
};</pre> |
|
|
426 |
|
|
|
427 |
|
|
|
428 |
<p> |
|
|
429 |
After <code>fireOnce</code> events have been <code>fire()</code>d, any subsequent (late) |
|
|
430 |
subscriptions are immediately executed. This can introduce race |
|
|
431 |
conditions, however, since subscribers might expect to be called at a later |
|
|
432 |
time, after the code that follows the subscription has also executed. In |
|
|
433 |
this case, you can configure <code>fireOnce</code> events with the <code>async</code> flag |
|
|
434 |
and post-<code>fire</code> subscriptions will be executed in a <code>setTimeout</code>, |
|
|
435 |
allowing all subsequent code to run before the late subscriber is notified. |
|
|
436 |
</p> |
|
|
437 |
|
|
|
438 |
<pre class="code prettyprint">// BEFORE |
|
|
439 |
Y.publish('myapp:ready', { |
|
|
440 |
fireOnce: true |
|
|
441 |
}); |
|
|
442 |
|
|
|
443 |
// ... elsewhere in the code |
|
|
444 |
// If myapp:ready has been fired, setStuffUp executes right now, but might |
|
|
445 |
// expect MyApp.Stuff to be created already. So, boom. |
|
|
446 |
Y.on('myapp:ready', setStuffUp); |
|
|
447 |
|
|
|
448 |
MyApp.Stuff = {}; |
|
|
449 |
|
|
|
450 |
// AFTER |
|
|
451 |
Y.publish('myapp:ready', { |
|
|
452 |
fireOnce: true, |
|
|
453 |
async : true |
|
|
454 |
}); |
|
|
455 |
|
|
|
456 |
// ... elsewhere in the code |
|
|
457 |
// Even if myapp:ready has been fired, setStuffUp will execute later. So, no boom |
|
|
458 |
Y.on('myapp:ready', setStuffUp); |
|
|
459 |
|
|
|
460 |
MyApp.Stuff = {};</pre> |
|
|
461 |
|
|
|
462 |
|
|
|
463 |
<h3 id="bubbling">Bubbling Events</h3> |
|
|
464 |
|
|
|
465 |
<p> |
|
|
466 |
Events that are configured with <code>emitFacade</code> support bubbling to other |
|
|
467 |
<code>EventTarget</code>s, allowing you to subscribe to them from other objects, much |
|
|
468 |
like DOM event bubbling. Add other <code>EventTarget</code>s to an instance's bubble |
|
|
469 |
path with <code>addTarget</code>. |
|
|
470 |
</p> |
|
|
471 |
|
|
|
472 |
<pre class="code prettyprint">function LeafNode() { ... } |
|
|
473 |
|
|
|
474 |
LeafNode.prototype.rename = function (newName) { |
|
|
475 |
var oldName = this.name; |
|
|
476 |
this.name = newName; |
|
|
477 |
|
|
|
478 |
this.fire("update", { |
|
|
479 |
prevVal: oldName, |
|
|
480 |
newVal : newName |
|
|
481 |
}); |
|
|
482 |
}; |
|
|
483 |
|
|
|
484 |
function TreeNode() { ... } |
|
|
485 |
|
|
|
486 |
TreeNode.prototype.add = function (node) { |
|
|
487 |
this._items.push(node); |
|
|
488 |
|
|
|
489 |
// The new child node's events will bubble to this TreeNode |
|
|
490 |
node.addTarget(this); |
|
|
491 |
}; |
|
|
492 |
|
|
|
493 |
Y.augment(LeafNode, Y.EventTarget, true, null, { emitFacade: true }); |
|
|
494 |
Y.augment(TreeNode, Y.EventTarget, true, null, { emitFacade: true }); |
|
|
495 |
|
|
|
496 |
var rootNode = new TreeNode("ROOT"), |
|
|
497 |
branchA = new TreeNode("branchA"), |
|
|
498 |
leaf1 = new LeafNode("leaf1"); |
|
|
499 |
|
|
|
500 |
rootNode.add(branchA); // ROOT |
|
|
501 |
rootNode.add( new LeafNode("leaf2") ); // / \ |
|
|
502 |
// branchA leaf2 |
|
|
503 |
branchA.add(leaf1); // / \ |
|
|
504 |
branchA.add( new LeafNode("leaf3") ); // leaf1 leaf3 |
|
|
505 |
|
|
|
506 |
// Subscribe to 'update' events from any leaf or tree node under root |
|
|
507 |
rootNode.on('update', function (e) { |
|
|
508 |
alert(e.prevVal + " has been renamed " + e.newVal); |
|
|
509 |
}); |
|
|
510 |
|
|
|
511 |
leaf1.rename("Flower!"); // ==> "leaf1 has been renamed Flower!"</pre> |
|
|
512 |
|
|
|
513 |
|
|
|
514 |
|
|
|
515 |
<h3 id="prefix">Event Prefixes</h3> |
|
|
516 |
|
|
|
517 |
<p> |
|
|
518 |
Individual events or all events fired by an <code>EventTarget</code> can be configured |
|
|
519 |
to include a prefix to help filter subscriptions to common event names by |
|
|
520 |
their origin. Prefixed event names look like <code>'prefix:eventName'</code>. |
|
|
521 |
</p> |
|
|
522 |
|
|
|
523 |
<p> |
|
|
524 |
Taking the <a href="#bubbling">code snippet above</a>, configuring a default |
|
|
525 |
<code>prefix</code> while augmenting the classes will allow for subscription to |
|
|
526 |
only <code>LeafNode</code> updates. |
|
|
527 |
</p> |
|
|
528 |
<pre class="code prettyprint">// All events fired by LeafNode instances will be prefixed with "leaf:" |
|
|
529 |
Y.augment(LeafNode, Y.EventTarget, true, null, { |
|
|
530 |
emitFacade: true, |
|
|
531 |
prefix: 'leaf' |
|
|
532 |
}); |
|
|
533 |
// ...and for TreeNodes, "tree:" |
|
|
534 |
Y.augment(TreeNode, Y.EventTarget, true, null, { |
|
|
535 |
emitFacade: true, |
|
|
536 |
prefix: 'tree' |
|
|
537 |
}); |
|
|
538 |
|
|
|
539 |
... |
|
|
540 |
|
|
|
541 |
// Listen specifically for changes from LeafNodes |
|
|
542 |
rootNode.on('leaf:update', function (e) { |
|
|
543 |
alert(e.prevVal + " has been renamed " + e.newVal); |
|
|
544 |
}); |
|
|
545 |
|
|
|
546 |
leaf1.rename("Flower!"); // ==> "leaf1 has been renamed Flower!" |
|
|
547 |
branchA.rename("Chewbacca!"); // (nothing)</pre> |
|
|
548 |
|
|
|
549 |
|
|
|
550 |
<p> |
|
|
551 |
Subscribing with prefixes is similar to |
|
|
552 |
<a href="../event/delegation.html">using DOM event delegation</a>, though it |
|
|
553 |
is done using <code>on()</code> rather than <code>delegate()</code>. |
|
|
554 |
</p> |
|
|
555 |
|
|
|
556 |
<p> |
|
|
557 |
Optionally, you can omit the prefix when subscribing on the object that |
|
|
558 |
fires the event. |
|
|
559 |
</p> |
|
|
560 |
|
|
|
561 |
<pre class="code prettyprint">// prefix is optional when subscribing on the firing object... |
|
|
562 |
leaf1.on('leaf:update', worksJustLike); |
|
|
563 |
leaf1.on('update', function (e) { |
|
|
564 |
e.type; // 'leaf:update' -- the event type will remain prefixed |
|
|
565 |
... |
|
|
566 |
}); |
|
|
567 |
|
|
|
568 |
// ...but prefixes are required from other objects |
|
|
569 |
rootNode.on('update', function (e) { |
|
|
570 |
// will not capture leaf:update events |
|
|
571 |
});</pre> |
|
|
572 |
|
|
|
573 |
|
|
|
574 |
<p> |
|
|
575 |
Subscribe to all events of a specific type, regardless of prefix, using the |
|
|
576 |
wildcard prefix <code>*:eventName</code>. |
|
|
577 |
</p> |
|
|
578 |
|
|
|
579 |
<pre class="code prettyprint">// Execute the callback if either the group object or one of its items fires an |
|
|
580 |
// `update` event |
|
|
581 |
rootNode.on('*:update', function (e) { |
|
|
582 |
switch (e.type) { |
|
|
583 |
case "leaf:update": ... |
|
|
584 |
case "tree:update": ... |
|
|
585 |
} |
|
|
586 |
});</pre> |
|
|
587 |
|
|
|
588 |
|
|
|
589 |
<h3 id="defaultFn">Adding Default Behavior</h3> |
|
|
590 |
|
|
|
591 |
<p> |
|
|
592 |
Custom events can be bound to behaviors just like DOM events (e.g. clicking |
|
|
593 |
on a link causes navigation to a new page). This is especially useful when |
|
|
594 |
doing |
|
|
595 |
<a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> |
|
|
596 |
operations that you want to expose to other objects in your system to |
|
|
597 |
prevent, alter, or enhance. |
|
|
598 |
</p> |
|
|
599 |
|
|
|
600 |
<p> |
|
|
601 |
Add a default behavior to an event by configuring the event's <code>defaultFn</code>. |
|
|
602 |
By convention, default functions are named <code>_def(the name of the event)Fn</code>. |
|
|
603 |
</p> |
|
|
604 |
|
|
|
605 |
<pre class="code prettyprint">function TreeNode(name) { |
|
|
606 |
this.name = name; |
|
|
607 |
this._items = []; |
|
|
608 |
|
|
|
609 |
// Event publishing is typically done during instantiation |
|
|
610 |
this.publish('add', { |
|
|
611 |
defaultFn: this._defAddFn |
|
|
612 |
}); |
|
|
613 |
} |
|
|
614 |
|
|
|
615 |
// Adding a child node is an interesting mutation operation. Move the mutation |
|
|
616 |
// logic from the method to a mutation event's default behavior |
|
|
617 |
TreeNode.prototype.add = function (node) { |
|
|
618 |
this.fire('add', { newNode: node }); |
|
|
619 |
}; |
|
|
620 |
|
|
|
621 |
// Default functions receive the event facade like other subscribers |
|
|
622 |
TreeNode.prototype._defAddFn = function (e) { |
|
|
623 |
this._items.push(e.newNode); |
|
|
624 |
|
|
|
625 |
e.newNode.addTarget(this); |
|
|
626 |
}; |
|
|
627 |
|
|
|
628 |
... |
|
|
629 |
|
|
|
630 |
branchA.add(leaf1); // without 'add' subscriptions, the behavior is the same</pre> |
|
|
631 |
|
|
|
632 |
|
|
|
633 |
<p> |
|
|
634 |
Unless configured with <code>preventable: false</code>, default behaviors can be |
|
|
635 |
disabled with <code>e.preventDefault()</code> just like the DOM. Unlike their DOM |
|
|
636 |
counterparts, though, event subscribers <em>can change facade |
|
|
637 |
properties</em> to alter the default behavior by way of effectively changing |
|
|
638 |
its input. |
|
|
639 |
</p> |
|
|
640 |
|
|
|
641 |
<pre class="code prettyprint">TreeNode.prototype.add = function (node) { |
|
|
642 |
this.fire('add', { |
|
|
643 |
newNode: node, |
|
|
644 |
bubbleEvents: true |
|
|
645 |
}); |
|
|
646 |
}; |
|
|
647 |
|
|
|
648 |
// Default functions receive the event facade like other subscribers |
|
|
649 |
TreeNode.prototype._defAddFn = function (e) { |
|
|
650 |
this._items.push(e.newNode); |
|
|
651 |
|
|
|
652 |
if (e.bubbleEvents) { |
|
|
653 |
e.newNode.addTarget(this); |
|
|
654 |
} |
|
|
655 |
}; |
|
|
656 |
|
|
|
657 |
... |
|
|
658 |
|
|
|
659 |
// You can prevent default behavior from anywhere along the bubble path |
|
|
660 |
rootNode.on('tree:add', function (e) { |
|
|
661 |
if (e.newNode.name === "Leafy") { |
|
|
662 |
e.preventDefault(); |
|
|
663 |
} else if (e.newNode.name === "James Bond") { |
|
|
664 |
e.bubbleEvents = false; // Shhhh |
|
|
665 |
} |
|
|
666 |
}); |
|
|
667 |
|
|
|
668 |
rootNode.add( new LeafNode("Leafy") ); // Node NOT added |
|
|
669 |
rootNode.add( new LeafNode("James Bond") ); // Node added without event bubbling</pre> |
|
|
670 |
|
|
|
671 |
|
|
|
672 |
<h3 id="broadcast">Broadcasting Events to Y or Between YUI instances</h3> |
|
|
673 |
|
|
|
674 |
<p> |
|
|
675 |
Event broadcasting is very similar to bubbling, but with some important |
|
|
676 |
distinctions: |
|
|
677 |
</p> |
|
|
678 |
|
|
|
679 |
<ol> |
|
|
680 |
<li> |
|
|
681 |
Broadcasting is specific to the YUI instance and the <code>Y.Global</code> shared |
|
|
682 |
<code>EventTarget</code> |
|
|
683 |
</li> |
|
|
684 |
<li>Events don't need to be configured with <code>emitFacade</code> to broadcast</li> |
|
|
685 |
<li>Broadcasting happens after the default behavior, which also means...</li> |
|
|
686 |
<li>Event behavior can't be prevented from broadcast subscribers</li> |
|
|
687 |
<li>Broadcast can be defaulted for all events for an <code>EventTarget</code></li> |
|
|
688 |
</ol> |
|
|
689 |
|
|
|
690 |
<p> |
|
|
691 |
Broadcasting is essentially a "fast track" bubbling configuration allowing |
|
|
692 |
you to specify that events can be subscribed to from the YUI instance (with |
|
|
693 |
<code>broadcast: 1</code>) or from <code>Y.Global</code> (with <code>broadcast: 2</code>). |
|
|
694 |
</p> |
|
|
695 |
|
|
|
696 |
<pre class="code prettyprint">// All events from instances of MyClass can be subscribed from Y.on() |
|
|
697 |
Y.augment(MyClass, Y.EventTarget, true, null, { |
|
|
698 |
emitFacade: true, |
|
|
699 |
prefix: 'awesome', |
|
|
700 |
broadcast: 1 |
|
|
701 |
}); |
|
|
702 |
|
|
|
703 |
// Respond to a 'thing' event from any instance of MyClass in the YUI sandbox |
|
|
704 |
Y.on('awesome:song', partyOn); |
|
|
705 |
|
|
|
706 |
var instance = new MyClass() |
|
|
707 |
|
|
|
708 |
instance.fire("song", { which: "Bohemian Rhapsody", whom: "Wayne" });</pre> |
|
|
709 |
|
|
|
710 |
|
|
|
711 |
<p> |
|
|
712 |
<code>Y.Global</code> is an <code>EventTarget</code> that is shared between all YUI instances, |
|
|
713 |
allowing cross-sandbox communication. To avoid feedback loops, it's best |
|
|
714 |
to add an instance identity to outgoing events and only respond to |
|
|
715 |
incoming events from other identities. |
|
|
716 |
</p> |
|
|
717 |
|
|
|
718 |
<pre class="code prettyprint">YUI().use('node', 'event-custom', function (Y) { |
|
|
719 |
var id = "Alpha Beta Base"; // probably Y.guid() would be safer |
|
|
720 |
|
|
|
721 |
Y.Global.on('message', function (e) { |
|
|
722 |
if (e.origin !== id) { |
|
|
723 |
alert("message received from " + e.origin + ": " + e.message); |
|
|
724 |
|
|
|
725 |
murdock.fire("message", { |
|
|
726 |
message: "We'll get you down. And down safe.", |
|
|
727 |
origin: id |
|
|
728 |
}); |
|
|
729 |
} |
|
|
730 |
}); |
|
|
731 |
|
|
|
732 |
function Character() { |
|
|
733 |
this.publish('message', { broadcast: 2 }); |
|
|
734 |
... |
|
|
735 |
} |
|
|
736 |
|
|
|
737 |
Y.augment(Character, Y.EventTarget, true, null, { |
|
|
738 |
emitFacade: true |
|
|
739 |
}); |
|
|
740 |
|
|
|
741 |
var murdock = new Character(); |
|
|
742 |
|
|
|
743 |
Y.one('#status').on('click', function () { |
|
|
744 |
murdock.fire("message", { |
|
|
745 |
message: "You're coming in too fast!", |
|
|
746 |
origin: id |
|
|
747 |
}); |
|
|
748 |
}); |
|
|
749 |
}); |
|
|
750 |
|
|
|
751 |
YUI().use('node', 'event-custom', function (OtherY) { |
|
|
752 |
var id = "Lunar Shuttle"; |
|
|
753 |
|
|
|
754 |
OtherY.Global.on('message', function (e) { |
|
|
755 |
if (e.origin !== id) { |
|
|
756 |
alert("message received from " + e.origin + ": " + e.message); |
|
|
757 |
} |
|
|
758 |
}); |
|
|
759 |
|
|
|
760 |
function Character() { |
|
|
761 |
this.publish('message', { broadcast: 2 }); |
|
|
762 |
} |
|
|
763 |
|
|
|
764 |
OtherY.augment(Character, OtherY.EventTarget, true, null, { |
|
|
765 |
emitFacade: true |
|
|
766 |
}); |
|
|
767 |
|
|
|
768 |
var striker = new Character() |
|
|
769 |
|
|
|
770 |
OtherY.one('#report').on('click', function () { |
|
|
771 |
striker.fire("message", { |
|
|
772 |
message: "She's beginning to crack up", |
|
|
773 |
origin: id |
|
|
774 |
}); |
|
|
775 |
}); |
|
|
776 |
});</pre> |
|
|
777 |
|
|
|
778 |
<button id="status" class="yui3-button">Come in, Lunar Shuttle</button> |
|
|
779 |
<button id="report" class="yui3-button">Can you read me, Alpha Beta?</button> |
|
|
780 |
|
|
|
781 |
<script> |
|
|
782 |
YUI().use('node', 'event-custom', function (Y) { |
|
|
783 |
var id = "Alpha Beta Base"; // probably Y.guid() would be safer |
|
|
784 |
|
|
|
785 |
Y.Global.on('message', function (e) { |
|
|
786 |
if (e.origin !== id) { |
|
|
787 |
alert("message received from " + e.origin + ": " + e.message); |
|
|
788 |
|
|
|
789 |
murdock.fire("message", { |
|
|
790 |
message: "We'll get you down. And down safe.", |
|
|
791 |
origin: id |
|
|
792 |
}); |
|
|
793 |
} |
|
|
794 |
}); |
|
|
795 |
|
|
|
796 |
function Character() { |
|
|
797 |
this.publish('message', { broadcast: 2 }); |
|
|
798 |
} |
|
|
799 |
|
|
|
800 |
Y.augment(Character, Y.EventTarget, true, null, { |
|
|
801 |
emitFacade: true |
|
|
802 |
}); |
|
|
803 |
|
|
|
804 |
var murdock = new Character(); |
|
|
805 |
|
|
|
806 |
Y.one('#status').on('click', function () { |
|
|
807 |
murdock.fire("message", { |
|
|
808 |
message: "You're coming in too fast!", |
|
|
809 |
origin: id |
|
|
810 |
}); |
|
|
811 |
}); |
|
|
812 |
}); |
|
|
813 |
|
|
|
814 |
YUI().use('node', 'event-custom', function (OtherY) { |
|
|
815 |
var id = "Lunar Shuttle"; |
|
|
816 |
|
|
|
817 |
OtherY.Global.on('message', function (e) { |
|
|
818 |
if (e.origin !== id) { |
|
|
819 |
alert("message received from " + e.origin + ": " + e.message); |
|
|
820 |
} |
|
|
821 |
}); |
|
|
822 |
|
|
|
823 |
function Character() { |
|
|
824 |
this.publish('message', { broadcast: 2 }); |
|
|
825 |
} |
|
|
826 |
|
|
|
827 |
OtherY.augment(Character, OtherY.EventTarget, true, null, { |
|
|
828 |
emitFacade: true |
|
|
829 |
}); |
|
|
830 |
|
|
|
831 |
var striker = new Character() |
|
|
832 |
|
|
|
833 |
OtherY.one('#report').on('click', function () { |
|
|
834 |
striker.fire("message", { |
|
|
835 |
message: "She's beginning to crack up", |
|
|
836 |
origin: id |
|
|
837 |
}); |
|
|
838 |
}); |
|
|
839 |
}); |
|
|
840 |
</script> |
|
|
841 |
|
|
|
842 |
<!--h3>Monitoring Events</h3> |
|
|
843 |
|
|
|
844 |
<p>TODO</p--> |
|
|
845 |
|
|
|
846 |
|
|
|
847 |
<h3 id="configs">Available Event Configurations and Defaults</h3> |
|
|
848 |
|
|
|
849 |
<p> |
|
|
850 |
Events can be configured with the following properties. Properties marked |
|
|
851 |
as "Class Configurable" can be passed to the <code>EventTarget</code> constructor |
|
|
852 |
configuration to default for all events. |
|
|
853 |
</p> |
|
|
854 |
|
|
|
855 |
<table> |
|
|
856 |
<thead> |
|
|
857 |
<tr> |
|
|
858 |
<th>Configuration</th> |
|
|
859 |
<th>Description</th> |
|
|
860 |
<th>Default</th> |
|
|
861 |
<th>Class Configurable?</th> |
|
|
862 |
</tr> |
|
|
863 |
</thead> |
|
|
864 |
<tbody> |
|
|
865 |
<tr> |
|
|
866 |
<td><code>prefix</code></td> |
|
|
867 |
<td> |
|
|
868 |
<code>e.type</code> will always include the configured prefix. |
|
|
869 |
<a href="#prefix">Details above</a>. |
|
|
870 |
</td> |
|
|
871 |
<td>(empty)</td> |
|
|
872 |
<td>YES</td> |
|
|
873 |
</tr> |
|
|
874 |
<tr> |
|
|
875 |
<td><code>context</code></td> |
|
|
876 |
<td> |
|
|
877 |
The default <code>this</code> object to execute callbacks with. Rarely set. |
|
|
878 |
</td> |
|
|
879 |
<td>The instance</td> |
|
|
880 |
<td>YES</td> |
|
|
881 |
</tr> |
|
|
882 |
<tr> |
|
|
883 |
<td><code>emitFacade</code></td> |
|
|
884 |
<td> |
|
|
885 |
If <code>true</code>, sends event facades to callbacks, allows bubbling and |
|
|
886 |
default functions, etc. This is commonly set to true for a class. |
|
|
887 |
<a href="#facade">Details above</a>. |
|
|
888 |
</td> |
|
|
889 |
<td><code>false</code></td> |
|
|
890 |
<td>YES</td> |
|
|
891 |
</tr> |
|
|
892 |
<tr> |
|
|
893 |
<td><code>fireOnce</code></td> |
|
|
894 |
<td> |
|
|
895 |
If <code>true</code>, events will only fire once. Subscriptions made after |
|
|
896 |
firing will be immediately executed. |
|
|
897 |
<a href="#once">Details above</a>. |
|
|
898 |
</td> |
|
|
899 |
<td><code>false</code></td> |
|
|
900 |
<td>YES</td> |
|
|
901 |
</tr> |
|
|
902 |
<!--tr> |
|
|
903 |
<td><code>monitored</code></td> |
|
|
904 |
<td> |
|
|
905 |
Allows you to subscribe to the event lifecycle moments (publish, |
|
|
906 |
fire, and subscribe) as separate events. |
|
|
907 |
<a href="#monitor">Details above</a>. |
|
|
908 |
</td> |
|
|
909 |
<td><code>false</code></td> |
|
|
910 |
<td>YES</td> |
|
|
911 |
</tr--> |
|
|
912 |
<tr> |
|
|
913 |
<td><code>broadcast</code></td> |
|
|
914 |
<td> |
|
|
915 |
<a href="#broadcast">Details above</a>. Fire the event from: |
|
|
916 |
<ul> |
|
|
917 |
<li><code>0</code> - Only the EventTarget instance</li> |
|
|
918 |
<li><code>1</code> - The EventTarget and the YUI instance (<code>Y</code>)</li> |
|
|
919 |
<li><code>2</code> - The EventTarget, <code>Y</code>, and <code>Y.Global</code></li> |
|
|
920 |
</ul> |
|
|
921 |
</td> |
|
|
922 |
<td>0</td> |
|
|
923 |
<td>YES</td> |
|
|
924 |
</tr> |
|
|
925 |
<tr> |
|
|
926 |
<td><code>bubbles</code></td> |
|
|
927 |
<td> |
|
|
928 |
For events configured to <code>emitFacade</code> allow bubbling events to |
|
|
929 |
other <code>EventTarget</code>s. |
|
|
930 |
</td> |
|
|
931 |
<td><code>true</code></td> |
|
|
932 |
<td>YES</td> |
|
|
933 |
</tr> |
|
|
934 |
<tr> |
|
|
935 |
<td><code>defaultFn</code></td> |
|
|
936 |
<td> |
|
|
937 |
Behavior associated with the event. Usually this is preventable |
|
|
938 |
(see <code>preventable</code> below). <a href="#defaultFn">Details above</a>. |
|
|
939 |
</td> |
|
|
940 |
<td>(none)</td> |
|
|
941 |
<td> </td> |
|
|
942 |
</tr> |
|
|
943 |
<tr> |
|
|
944 |
<td><code>preventable</code></td> |
|
|
945 |
<td> |
|
|
946 |
If set to <code>false</code>, <code>e.preventDefault()</code> will not disable execution |
|
|
947 |
of the event's <code>defaultFn</code>. |
|
|
948 |
</td> |
|
|
949 |
<td><code>true</code></td> |
|
|
950 |
<td> </td> |
|
|
951 |
</tr> |
|
|
952 |
<tr> |
|
|
953 |
<td><code>preventedFn</code></td> |
|
|
954 |
<td> |
|
|
955 |
<p> |
|
|
956 |
Behavior associated with the event when <code>e.preventDefault()</code> is |
|
|
957 |
called from a subscriber. Use this function to reset partially |
|
|
958 |
applied transactional state. |
|
|
959 |
</p> |
|
|
960 |
<p>Incompatible with <code>preventable: false</code>.</p> |
|
|
961 |
</td> |
|
|
962 |
<td>(none)</td> |
|
|
963 |
<td> </td> |
|
|
964 |
</tr> |
|
|
965 |
<tr> |
|
|
966 |
<td><code>stoppedFn</code></td> |
|
|
967 |
<td> |
|
|
968 |
Behavior associated with the event when <code>e.stopPropagation()</code> is |
|
|
969 |
called from a subscriber. Seldom used. |
|
|
970 |
</td> |
|
|
971 |
<td>(none)</td> |
|
|
972 |
<td> </td> |
|
|
973 |
</tr> |
|
|
974 |
<tr> |
|
|
975 |
<td><code>async</code></td> |
|
|
976 |
<td> |
|
|
977 |
Only applicable to events also configured with <code>fireOnce: true</code>. |
|
|
978 |
If <code>true</code>, new subscriptions to this event after it has already |
|
|
979 |
been fired will be queued to execute in a <code>setTimeout</code> instead of |
|
|
980 |
immediately (synchronously). |
|
|
981 |
</td> |
|
|
982 |
<td>false</td> |
|
|
983 |
<td> </td> |
|
|
984 |
</tr> |
|
|
985 |
</tbody> |
|
|
986 |
</table> |
|
|
987 |
|
|
|
988 |
<h3 id="after">The "after" phase</h3> |
|
|
989 |
|
|
|
990 |
<p> |
|
|
991 |
Unlike DOM events, custom events also expose an "after" phase that |
|
|
992 |
corresponds to the time immediately after an event's <a |
|
|
993 |
href="#defaultFn">default behavior</a> executes. Subscribe to an event's |
|
|
994 |
"after" phase with the <code>after(...)</code> method. The signature is the same as |
|
|
995 |
<code>on(...)</code>. |
|
|
996 |
</p> |
|
|
997 |
|
|
|
998 |
<pre class="code prettyprint">rootNode.after('tree:add', calc.updateTotals, calc);</pre> |
|
|
999 |
|
|
|
1000 |
|
|
|
1001 |
<p> |
|
|
1002 |
The primary benefit of using <code>after()</code> subscriptions over <code>on()</code> |
|
|
1003 |
subscriptions is that if any <code>on()</code> subscribers call <code>e.preventDefault()</code>, |
|
|
1004 |
neither the event's configured <code>defaultFn</code> <em>nor the <code>after()</code> |
|
|
1005 |
subscribers</em> will be executed. If an <code>after()</code> subscription is |
|
|
1006 |
executed, you know that the <code>defaultFn</code> did as well. |
|
|
1007 |
</p> |
|
|
1008 |
|
|
|
1009 |
<p> |
|
|
1010 |
<strong>Use <code>after()</code> to subscribe to events with a default behavior when |
|
|
1011 |
you want to react to the event with a side effect.</strong> |
|
|
1012 |
</p> |
|
|
1013 |
|
|
|
1014 |
<p> |
|
|
1015 |
<strong>Use <code>on()</code> to subscribe to events if you need to prevent or alter |
|
|
1016 |
the default behavior or if they don't have default behavior.</strong> |
|
|
1017 |
</p> |
|
|
1018 |
|
|
|
1019 |
<h2 id="event-lifecycle">Event Lifecycle</h2> |
|
|
1020 |
|
|
|
1021 |
<p>The order of operations when firing an event is as follows:</p> |
|
|
1022 |
|
|
|
1023 |
<h3 id="simple-event-lifecycle">Simple Events (no facade)</h3> |
|
|
1024 |
|
|
|
1025 |
<ol> |
|
|
1026 |
<li><code>on()</code> subscribers are executed</li> |
|
|
1027 |
<li><code>after()</code> subscribers are executed</li> |
|
|
1028 |
<li><code>Y.on()</code> broadcast subscribers are executed.</li> |
|
|
1029 |
<li><code>Y.after()</code> broadcast subscribers are executed.</li> |
|
|
1030 |
<li><code>Y.Global.on()</code> broadcast subscribers are executed.</li> |
|
|
1031 |
<li><code>Y.Global.after()</code> broadcast subscribers are executed.</li> |
|
|
1032 |
</ol> |
|
|
1033 |
|
|
|
1034 |
<p> |
|
|
1035 |
If an <code>on()</code> or <code>after()</code> subscriber returns <code>false</code>, no more subscribers |
|
|
1036 |
will be notified. |
|
|
1037 |
</p> |
|
|
1038 |
|
|
|
1039 |
<h3 id="complex-event-lifecycle">Complex Events (with facade)</h3> |
|
|
1040 |
|
|
|
1041 |
<ol> |
|
|
1042 |
<li><code>on()</code> subscribers are executed</li> |
|
|
1043 |
<li> |
|
|
1044 |
<code>on()</code> subscribers for each bubble target and their respective targets |
|
|
1045 |
are executed until all targets' bubble paths are walked or a subscriber |
|
|
1046 |
stops the propagation of the event. |
|
|
1047 |
</li> |
|
|
1048 |
<li> |
|
|
1049 |
If the event was prevented, any configured <code>preventedFn</code> will execute. |
|
|
1050 |
</li> |
|
|
1051 |
<li>If not prevented, any configured <code>defaultFn</code> will execute.</li> |
|
|
1052 |
<li>If bubbling was stopped, any configured <code>stoppedFn</code> will execute.</li> |
|
|
1053 |
<li><code>Y.on()</code> broadcast subscribers are executed.</li> |
|
|
1054 |
<li><code>Y.after()</code> broadcast subscribers are executed.</li> |
|
|
1055 |
<li><code>Y.Global.on()</code> broadcast subscribers are executed.</li> |
|
|
1056 |
<li><code>Y.Global.after()</code> broadcast subscribers are executed.</li> |
|
|
1057 |
<li><code>after()</code> subscribers are executed.</li> |
|
|
1058 |
<li> |
|
|
1059 |
<code>after()</code> subscribers for each bubble target and their respective |
|
|
1060 |
targets are executed. |
|
|
1061 |
</li> |
|
|
1062 |
</ol> |
|
|
1063 |
|
|
|
1064 |
<p> |
|
|
1065 |
The flow can be interrupted by <code>on()</code> subscribers doing any of these |
|
|
1066 |
things: |
|
|
1067 |
</p> |
|
|
1068 |
|
|
|
1069 |
<dl> |
|
|
1070 |
<dt><code>e.preventDefault()</code></dt> |
|
|
1071 |
<dd> |
|
|
1072 |
<ol> |
|
|
1073 |
<li>The <code>defaultFn</code> will not be executed</li> |
|
|
1074 |
<li>The <code>preventedFn</code> will execute</li> |
|
|
1075 |
<li>No <code>after()</code> subscriptions will be executed</li> |
|
|
1076 |
</ol> |
|
|
1077 |
</dd> |
|
|
1078 |
<dt><code>e.stopPropagation()</code></dt> |
|
|
1079 |
<dd> |
|
|
1080 |
<ol> |
|
|
1081 |
<li>The remainder of subscribers at this <code>EventTarget</code> <strong>WILL</strong> execute</li> |
|
|
1082 |
<li>No bubble targets of this <code>EventTarget</code> will be notified</li> |
|
|
1083 |
<li>The <code>stoppedFn</code> will execute</li> |
|
|
1084 |
<li>The <code>defaultFn</code> and <code>after()</code> subscribers will execute</li> |
|
|
1085 |
</ol> |
|
|
1086 |
</dd> |
|
|
1087 |
<dt><code>e.stopImmediatePropagation()</code></dt> |
|
|
1088 |
<dd> |
|
|
1089 |
Same as <code>e.stopPropagation()</code> except no more subscribers at this |
|
|
1090 |
<code>EventTarget</code> will execute. |
|
|
1091 |
</dd> |
|
|
1092 |
<dt><code>e.halt()</code></dt> |
|
|
1093 |
<dd> |
|
|
1094 |
Same as <code>e.preventDefault()</code> plus <code>e.stopPropagation()</code>. |
|
|
1095 |
</dd> |
|
|
1096 |
<dt><code>e.halt(true)</code></dt> |
|
|
1097 |
<dd> |
|
|
1098 |
Same as <code>e.preventDefault()</code> plus <code>e.stopImmediatePropagation()</code>. |
|
|
1099 |
</dd> |
|
|
1100 |
<dt><code>return false</code></dt> |
|
|
1101 |
<dd>Same as <code>e.halt(true)</code>. Not recommended. Use the API methods.</dd> |
|
|
1102 |
</dl> |
|
|
1103 |
|
|
|
1104 |
<!--h2 class="no-toc">Subscribing to Object Methods with <code>Y.Do.*</code></h2> |
|
|
1105 |
|
|
|
1106 |
<h3 class="no-toc">Before and After</h3> |
|
|
1107 |
|
|
|
1108 |
<h3 class="no-toc">Altering the Wrapped Method Behavior</h3> |
|
|
1109 |
|
|
|
1110 |
<h3 class="no-toc"><code>EventTarget</code> API methods</h3> |
|
|
1111 |
|
|
|
1112 |
<p> |
|
|
1113 |
TODO |
|
|
1114 |
</p--> |
|
|
1115 |
</div> |
|
|
1116 |
</div> |
|
|
1117 |
</div> |
|
|
1118 |
|
|
|
1119 |
<div class="yui3-u-1-4"> |
|
|
1120 |
<div class="sidebar"> |
|
|
1121 |
|
|
|
1122 |
<div id="toc" class="sidebox"> |
|
|
1123 |
<div class="hd"> |
|
|
1124 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
1125 |
</div> |
|
|
1126 |
|
|
|
1127 |
<div class="bd"> |
|
|
1128 |
<ul class="toc"> |
|
|
1129 |
<li> |
|
|
1130 |
<a href="#getting-started">Getting Started</a> |
|
|
1131 |
</li> |
|
|
1132 |
<li> |
|
|
1133 |
<a href="#video-overview">Video Overview</a> |
|
|
1134 |
</li> |
|
|
1135 |
<li> |
|
|
1136 |
<a href="#the-basics">The Basics</a> |
|
|
1137 |
<ul class="toc"> |
|
|
1138 |
<li> |
|
|
1139 |
<a href="#subscribing-to-events">Subscribing to Events</a> |
|
|
1140 |
</li> |
|
|
1141 |
<li> |
|
|
1142 |
<a href="#fire">Firing Events</a> |
|
|
1143 |
</li> |
|
|
1144 |
<li> |
|
|
1145 |
<a href="#callback-arguments-and-event-facades">Callback arguments and event facades</a> |
|
|
1146 |
</li> |
|
|
1147 |
<li> |
|
|
1148 |
<a href="#detaching-event-subscriptions">Detaching Event Subscriptions</a> |
|
|
1149 |
</li> |
|
|
1150 |
<li> |
|
|
1151 |
<a href="#extend-event-target">Extending EventTarget</a> |
|
|
1152 |
</li> |
|
|
1153 |
</ul> |
|
|
1154 |
</li> |
|
|
1155 |
<li> |
|
|
1156 |
<a href="#publishing-events">Publishing Events</a> |
|
|
1157 |
<ul class="toc"> |
|
|
1158 |
<li> |
|
|
1159 |
<a href="#facade">Event Facades</a> |
|
|
1160 |
</li> |
|
|
1161 |
<li> |
|
|
1162 |
<a href="#once"><code>fireOnce</code> Events</a> |
|
|
1163 |
</li> |
|
|
1164 |
<li> |
|
|
1165 |
<a href="#bubbling">Bubbling Events</a> |
|
|
1166 |
</li> |
|
|
1167 |
<li> |
|
|
1168 |
<a href="#prefix">Event Prefixes</a> |
|
|
1169 |
</li> |
|
|
1170 |
<li> |
|
|
1171 |
<a href="#defaultFn">Adding Default Behavior</a> |
|
|
1172 |
</li> |
|
|
1173 |
<li> |
|
|
1174 |
<a href="#broadcast">Broadcasting Events to Y or Between YUI instances</a> |
|
|
1175 |
</li> |
|
|
1176 |
<li> |
|
|
1177 |
<a href="#configs">Available Event Configurations and Defaults</a> |
|
|
1178 |
</li> |
|
|
1179 |
<li> |
|
|
1180 |
<a href="#after">The "after" phase</a> |
|
|
1181 |
</li> |
|
|
1182 |
</ul> |
|
|
1183 |
</li> |
|
|
1184 |
<li> |
|
|
1185 |
<a href="#event-lifecycle">Event Lifecycle</a> |
|
|
1186 |
<ul class="toc"> |
|
|
1187 |
<li> |
|
|
1188 |
<a href="#simple-event-lifecycle">Simple Events (no facade)</a> |
|
|
1189 |
</li> |
|
|
1190 |
<li> |
|
|
1191 |
<a href="#complex-event-lifecycle">Complex Events (with facade)</a> |
|
|
1192 |
</li> |
|
|
1193 |
</ul> |
|
|
1194 |
</li> |
|
|
1195 |
</ul> |
|
|
1196 |
</div> |
|
|
1197 |
</div> |
|
|
1198 |
|
|
|
1199 |
|
|
|
1200 |
|
|
|
1201 |
<div class="sidebox"> |
|
|
1202 |
<div class="hd"> |
|
|
1203 |
<h2 class="no-toc">Examples</h2> |
|
|
1204 |
</div> |
|
|
1205 |
|
|
|
1206 |
<div class="bd"> |
|
|
1207 |
<ul class="examples"> |
|
|
1208 |
|
|
|
1209 |
|
|
|
1210 |
<li data-description="Publish an event with a default behavior, as well as behaviors for reacting to preventing the default or stopping bubbling."> |
|
|
1211 |
<a href="flow-example.html">Custom Event Bubbling and Behaviors</a> |
|
|
1212 |
</li> |
|
|
1213 |
|
|
|
1214 |
|
|
|
1215 |
</ul> |
|
|
1216 |
</div> |
|
|
1217 |
</div> |
|
|
1218 |
|
|
|
1219 |
|
|
|
1220 |
|
|
|
1221 |
</div> |
|
|
1222 |
</div> |
|
|
1223 |
</div> |
|
|
1224 |
</div> |
|
|
1225 |
|
|
|
1226 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
1227 |
<script>prettyPrint();</script> |
|
|
1228 |
|
|
|
1229 |
<script> |
|
|
1230 |
YUI.Env.Tests = { |
|
|
1231 |
examples: [], |
|
|
1232 |
project: '../assets', |
|
|
1233 |
assets: '../assets/event-custom', |
|
|
1234 |
name: 'event-custom', |
|
|
1235 |
title: 'EventTarget', |
|
|
1236 |
newWindow: '', |
|
|
1237 |
auto: false |
|
|
1238 |
}; |
|
|
1239 |
YUI.Env.Tests.examples.push('flow-example'); |
|
|
1240 |
|
|
|
1241 |
</script> |
|
|
1242 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
1243 |
|
|
|
1244 |
|
|
|
1245 |
|
|
|
1246 |
</body> |
|
|
1247 |
</html> |