|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Intro to Event Delegation</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>Intro to Event Delegation</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><style> |
|
|
31 |
#todo-example { |
|
|
32 |
margin-top: 2em; |
|
|
33 |
max-width: 300px; |
|
|
34 |
*width: 300px; |
|
|
35 |
border: 2px solid #aaa; |
|
|
36 |
padding: 10px; |
|
|
37 |
} |
|
|
38 |
#todo-example legend { |
|
|
39 |
color: #888; |
|
|
40 |
position: relative; |
|
|
41 |
bottom: .4em; |
|
|
42 |
font-size: 1.3em; |
|
|
43 |
*position: auto; |
|
|
44 |
} |
|
|
45 |
#todo-example ol { |
|
|
46 |
list-style: decimal; |
|
|
47 |
padding-left: 0; |
|
|
48 |
margin: 0 0 1em; |
|
|
49 |
} |
|
|
50 |
#todo-example li { |
|
|
51 |
clear: right; |
|
|
52 |
margin-left: 25px; |
|
|
53 |
margin-right: 30px; |
|
|
54 |
*margin-left: 0; |
|
|
55 |
padding: 0; |
|
|
56 |
} |
|
|
57 |
#todo-example .delete-todo { |
|
|
58 |
float: right; |
|
|
59 |
position: relative; |
|
|
60 |
right: -30px; |
|
|
61 |
background: url(../assets/event/remove.png) no-repeat right; |
|
|
62 |
border: 0 none; |
|
|
63 |
width: 30px; |
|
|
64 |
overflow: hidden; |
|
|
65 |
text-indent: 30px; |
|
|
66 |
opacity: 0.5; |
|
|
67 |
*filter: alpha(opacity=50); |
|
|
68 |
} |
|
|
69 |
#todo-example input { |
|
|
70 |
width: 220px; |
|
|
71 |
border: 1px solid #aaa; |
|
|
72 |
xbackground: url(../assets/event/add.png) no-repeat right; |
|
|
73 |
padding: 5px 0 5px 5px; |
|
|
74 |
} |
|
|
75 |
</style> |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
<h2 id="understanding-the-problem">Understanding the problem</h2> |
|
|
79 |
|
|
|
80 |
<p>Consider the following Todo List widget:</p> |
|
|
81 |
|
|
|
82 |
<fieldset id="todo-example"> |
|
|
83 |
<legend>Todo List</legend> |
|
|
84 |
<ol> |
|
|
85 |
<li><button class="delete-todo" title="remove">remove</button>Read YUI documentation</li> |
|
|
86 |
<li><button class="delete-todo" title="remove">remove</button>Build awesome web app</li> |
|
|
87 |
<li><button class="delete-todo" title="remove">remove</button>Profit!</li> |
|
|
88 |
</ol> |
|
|
89 |
<input id="todo"> <button id="add-todo" type="button">add</button> |
|
|
90 |
</fieldset> |
|
|
91 |
<script> |
|
|
92 |
YUI().use('node-event-delegate', 'event-key', function (Y) { |
|
|
93 |
var todoList = Y.one('#todo-example ol'), |
|
|
94 |
newTask = Y.one('#todo'); |
|
|
95 |
|
|
|
96 |
todoList.delegate('click', function () { |
|
|
97 |
this.ancestor('li').remove(); |
|
|
98 |
}, 'button'); |
|
|
99 |
|
|
|
100 |
function addTodo() { |
|
|
101 |
todoList.append( |
|
|
102 |
'<li><button class="delete-todo">remove</button>' + |
|
|
103 |
newTask.get('value') + |
|
|
104 |
'</li>'); |
|
|
105 |
|
|
|
106 |
newTask.set('value', ''); |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
Y.one('#add-todo').on('click', addTodo); |
|
|
110 |
newTask.on('key', addTodo, 'enter'); |
|
|
111 |
}); |
|
|
112 |
</script> |
|
|
113 |
|
|
|
114 |
<p>All tasks are given a "remove" button. When new tasks are added, they |
|
|
115 |
should get a remove button that removes that task. Here's the markup for |
|
|
116 |
this:</p> |
|
|
117 |
|
|
|
118 |
<pre class="code prettyprint"><fieldset id="todo-example"> |
|
|
119 |
<legend>Todo List</legend> |
|
|
120 |
<ol> |
|
|
121 |
<li><button class="delete-todo">remove</button>Read YUI documentation</li> |
|
|
122 |
<li><button class="delete-todo">remove</button>Build awesome web app</li> |
|
|
123 |
<li><button class="delete-todo">remove</button>Profit!</li> |
|
|
124 |
</ol> |
|
|
125 |
<input id="todo"> <button id="add-todo" type="button">add</button> |
|
|
126 |
</fieldset></pre> |
|
|
127 |
|
|
|
128 |
|
|
|
129 |
<p>In the old days, you would have four click subscriptions:</p> |
|
|
130 |
|
|
|
131 |
<ol> |
|
|
132 |
<li>The remove button for #1</li> |
|
|
133 |
<li>The remove button for #2</li> |
|
|
134 |
<li>The remove button for #3</li> |
|
|
135 |
<li>The add button for creating new tasks</li> |
|
|
136 |
</ol> |
|
|
137 |
|
|
|
138 |
<p>When a user types in a new task and clicks the <em>add</em> button, a new |
|
|
139 |
<li> and corresponding <button> are created, and a fifth click |
|
|
140 |
subscription is added, one for the new button. The callback for the remove |
|
|
141 |
buttons could be unique for each button, or a generic function that determined |
|
|
142 |
which item to remove based on some other info from the event or button.</p> |
|
|
143 |
|
|
|
144 |
<p>When a user clicks on one of the remove buttons, the item is removed. The |
|
|
145 |
associated click event subscription is left in the system, taking up memory. |
|
|
146 |
So to solve this, maybe the event subscription is detached before the item is |
|
|
147 |
removed. Now there are four initial subscriptions and additional logic to |
|
|
148 |
properly detach subscriptions before items are removed.</p> |
|
|
149 |
|
|
|
150 |
<p>Over time, the number of items on the todo list grows, and so the number of |
|
|
151 |
subscriptions in the system, and thus memory consumed, grows with it. |
|
|
152 |
Additionally, if at some point, the entire list needs to be cleared, that's a |
|
|
153 |
lot of subscriptions to detach before it's ok to flush the list's |
|
|
154 |
<code>innerHTML</code>.</p> |
|
|
155 |
|
|
|
156 |
<h2 id="what-is-event-delegation">What is event delegation?</h2> |
|
|
157 |
|
|
|
158 |
<p>Event delegation is a way to reduce the number of subscriptions used to |
|
|
159 |
support this system. In the example case, only two click subscriptions are |
|
|
160 |
needed: one for the add button, and one for every remove button click. The |
|
|
161 |
second one is the <em>delegated subscription</em>. Here's how to think about |
|
|
162 |
it:</p> |
|
|
163 |
|
|
|
164 |
<p>The key to event delegation is understanding that a click on a remove button |
|
|
165 |
is also a click on</p> |
|
|
166 |
<ul> |
|
|
167 |
<li>the list item that the button is in</li> |
|
|
168 |
<li>the list itself</li> |
|
|
169 |
<li>the <fieldset> that the list is in</li> |
|
|
170 |
<li>etc up to the <body> and finally the <code>document</code><a |
|
|
171 |
href="#footnote1">[1]</a></li> |
|
|
172 |
</ul> |
|
|
173 |
|
|
|
174 |
<p>Instead of subscribing to the button's "click" event, <em>you can subscribe |
|
|
175 |
to the list's "click" event</em><a href="#footnote2">[2]</a>.</p> |
|
|
176 |
|
|
|
177 |
<h2 id="you-clicked-somewhere-but-where">You clicked somewhere, but <em>where</em>?</h2> |
|
|
178 |
|
|
|
179 |
<p>When you click anywhere on the document, the browser dispatches a click |
|
|
180 |
event that is assigned an <code>e.target</code> property corresponding to <em>the element |
|
|
181 |
that triggered the event</em>. For example, if you click on "Profit!", the |
|
|
182 |
event originated from the <li> with "Profit!" in it, so <code>e.target</code> will |
|
|
183 |
be that <li> element<a href="#footnote2">[3]</a>.</p> |
|
|
184 |
|
|
|
185 |
<p>With these two bits of information, we can create a single click |
|
|
186 |
subscription to respond to every button click in the Todo list.</p> |
|
|
187 |
|
|
|
188 |
<pre class="code prettyprint">function handleClick(e) { |
|
|
189 |
// look at e.target |
|
|
190 |
} |
|
|
191 |
|
|
|
192 |
Y.one('#todo-example ol').on('click', handleClick);</pre> |
|
|
193 |
|
|
|
194 |
|
|
|
195 |
<p>Now since there are no subscriptions tied directly to the individual |
|
|
196 |
buttons, we can add new items to the list without needing to add more |
|
|
197 |
subscriptions. Similarly, we can remove items or even clear the list's |
|
|
198 |
<code>innerHTML</code> without needing to detach subscriptions because there aren't any |
|
|
199 |
subscriptions inside the list to clear.</p> |
|
|
200 |
|
|
|
201 |
<h2 id="more-work-in-the-event-subscriber">More work in the event subscriber</h2> |
|
|
202 |
|
|
|
203 |
<p>Since any click inside the list is now triggering the event subscriber, it |
|
|
204 |
will be executed for button clicks, but also for clicks on the task item's text |
|
|
205 |
(e.g. "Profit!"). To make sure this click happened on a button, we need to |
|
|
206 |
inspect <code>e.target</code> to make sure it is a button.</p> |
|
|
207 |
|
|
|
208 |
<pre class="code prettyprint">function handleClick(e) { |
|
|
209 |
if (e.target.get('tagName').toLowerCase() === 'button') { |
|
|
210 |
// remove the item |
|
|
211 |
} |
|
|
212 |
}</pre> |
|
|
213 |
|
|
|
214 |
|
|
|
215 |
<p>This can start to get tricky when you're triggering on an element that can |
|
|
216 |
contain children. For example, if there were no buttons, but instead you |
|
|
217 |
wanted to remove items just by clicking on the <li>, you'd need to check |
|
|
218 |
if <code>e.target</code> was an <li>. But if it's not, you have to look at |
|
|
219 |
<code>e.target</code>'s <code>parentNode</code> and potentially that node's <code>parentNode</code> and so on, |
|
|
220 |
because <code>e.target</code> will always refer to the most specific element that received |
|
|
221 |
the click. This can amount to a lot of filtering code wrapping the item |
|
|
222 |
removal logic, which hinders the readability of your app.</p> |
|
|
223 |
|
|
|
224 |
<h2 id="let-nodedelegate-do-the-work-for-you">Let <code>node.delegate(...)</code> do the work for you</h2> |
|
|
225 |
|
|
|
226 |
<p>This is where <a href="../event/#event-delegation"><code>node.delegate(...)</code></a> |
|
|
227 |
comes in. <code>node.delegate(...)</code> boils down the filtering logic to a css |
|
|
228 |
selector, passed as the third argument. The subscribed callback will only |
|
|
229 |
execute if the event originated from an element that matches (or is contained |
|
|
230 |
in an element that matches) this css selector. This allows the code to power |
|
|
231 |
our Todo widget to look like this:</p> |
|
|
232 |
|
|
|
233 |
<pre class="code prettyprint">YUI().use('node-event-delegate', 'event-key', function (Y) { |
|
|
234 |
var todoList = Y.one('#todo-example ol'), |
|
|
235 |
newTask = Y.one('#todo'); |
|
|
236 |
|
|
|
237 |
// clicks inside the todo list on a <button> element will cause the |
|
|
238 |
// button's containing <li> to be removed |
|
|
239 |
todoList.delegate('click', function () { |
|
|
240 |
this.ancestor('li').remove(); |
|
|
241 |
}, 'button'); |
|
|
242 |
|
|
|
243 |
// Adding a new task is only appending a list item |
|
|
244 |
function addTodo() { |
|
|
245 |
todoList.append( |
|
|
246 |
'<li><button class="delete-todo">remove</button>' + |
|
|
247 |
newTask.get('value') + |
|
|
248 |
'</li>'); |
|
|
249 |
|
|
|
250 |
newTask.set('value', ''); |
|
|
251 |
} |
|
|
252 |
|
|
|
253 |
Y.one('#add-todo').on('click', addTodo); |
|
|
254 |
newTask.on('key', addTodo, 'enter'); // enter also adds todo (see event-key) |
|
|
255 |
});</pre> |
|
|
256 |
|
|
|
257 |
|
|
|
258 |
<hr> |
|
|
259 |
|
|
|
260 |
<h3 id="footnotes">Footnotes</h3> |
|
|
261 |
|
|
|
262 |
<ol id="footnotes"> |
|
|
263 |
<li id="footnote1">If there are click subscriptions at multiple points in |
|
|
264 |
the DOM heirarchy, they will be executed in order from most specific (the |
|
|
265 |
button) to least specific (document) unless <code>e.stopPropagation()</code> is |
|
|
266 |
called along the line. This will prevent subscriptions from elements higher |
|
|
267 |
up the parent axis from executing.</li> |
|
|
268 |
|
|
|
269 |
<li id="footnote2">We're using the "click" event here, but this all applies |
|
|
270 |
to other events as well.</li> |
|
|
271 |
|
|
|
272 |
<li id="footnote3">Actually the event originated from the text node inside |
|
|
273 |
the <li>, but IE reports the origin (the <code>srcElement</code> in IE) as the |
|
|
274 |
<li>, which is probably what developers want, anyway. YUI fixes |
|
|
275 |
<code>e.target</code> to bet the element for browsers that report it as the text |
|
|
276 |
node.</li> |
|
|
277 |
</ol> |
|
|
278 |
</div> |
|
|
279 |
</div> |
|
|
280 |
</div> |
|
|
281 |
|
|
|
282 |
<div class="yui3-u-1-4"> |
|
|
283 |
<div class="sidebar"> |
|
|
284 |
|
|
|
285 |
<div id="toc" class="sidebox"> |
|
|
286 |
<div class="hd"> |
|
|
287 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
288 |
</div> |
|
|
289 |
|
|
|
290 |
<div class="bd"> |
|
|
291 |
<ul class="toc"> |
|
|
292 |
<li> |
|
|
293 |
<a href="#understanding-the-problem">Understanding the problem</a> |
|
|
294 |
</li> |
|
|
295 |
<li> |
|
|
296 |
<a href="#what-is-event-delegation">What is event delegation?</a> |
|
|
297 |
</li> |
|
|
298 |
<li> |
|
|
299 |
<a href="#you-clicked-somewhere-but-where">You clicked somewhere, but <em>where</em>?</a> |
|
|
300 |
</li> |
|
|
301 |
<li> |
|
|
302 |
<a href="#more-work-in-the-event-subscriber">More work in the event subscriber</a> |
|
|
303 |
</li> |
|
|
304 |
<li> |
|
|
305 |
<a href="#let-nodedelegate-do-the-work-for-you">Let <code>node.delegate(...)</code> do the work for you</a> |
|
|
306 |
<ul class="toc"> |
|
|
307 |
<li> |
|
|
308 |
<a href="#footnotes">Footnotes</a> |
|
|
309 |
</li> |
|
|
310 |
</ul> |
|
|
311 |
</li> |
|
|
312 |
</ul> |
|
|
313 |
</div> |
|
|
314 |
</div> |
|
|
315 |
|
|
|
316 |
|
|
|
317 |
|
|
|
318 |
<div class="sidebox"> |
|
|
319 |
<div class="hd"> |
|
|
320 |
<h2 class="no-toc">Examples</h2> |
|
|
321 |
</div> |
|
|
322 |
|
|
|
323 |
<div class="bd"> |
|
|
324 |
<ul class="examples"> |
|
|
325 |
|
|
|
326 |
|
|
|
327 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
328 |
<a href="basic-example.html">Simple DOM Events</a> |
|
|
329 |
</li> |
|
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed."> |
|
|
334 |
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a> |
|
|
335 |
</li> |
|
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events"> |
|
|
340 |
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a> |
|
|
341 |
</li> |
|
|
342 |
|
|
|
343 |
|
|
|
344 |
|
|
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
|
348 |
|
|
|
349 |
|
|
|
350 |
|
|
|
351 |
|
|
|
352 |
|
|
|
353 |
|
|
|
354 |
</ul> |
|
|
355 |
</div> |
|
|
356 |
</div> |
|
|
357 |
|
|
|
358 |
|
|
|
359 |
|
|
|
360 |
<div class="sidebox"> |
|
|
361 |
<div class="hd"> |
|
|
362 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
363 |
</div> |
|
|
364 |
|
|
|
365 |
<div class="bd"> |
|
|
366 |
<ul class="examples"> |
|
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
|
370 |
|
|
|
371 |
|
|
|
372 |
|
|
|
373 |
|
|
|
374 |
|
|
|
375 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
376 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
377 |
</li> |
|
|
378 |
|
|
|
379 |
|
|
|
380 |
|
|
|
381 |
<li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
|
382 |
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a> |
|
|
383 |
</li> |
|
|
384 |
|
|
|
385 |
|
|
|
386 |
|
|
|
387 |
<li data-description="Example Photo Browser application."> |
|
|
388 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
389 |
</li> |
|
|
390 |
|
|
|
391 |
|
|
|
392 |
|
|
|
393 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
394 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
395 |
</li> |
|
|
396 |
|
|
|
397 |
|
|
|
398 |
|
|
|
399 |
<li data-description="Use IO to request data over HTTP."> |
|
|
400 |
<a href="../io/get.html">HTTP GET to request data</a> |
|
|
401 |
</li> |
|
|
402 |
|
|
|
403 |
|
|
|
404 |
</ul> |
|
|
405 |
</div> |
|
|
406 |
</div> |
|
|
407 |
|
|
|
408 |
</div> |
|
|
409 |
</div> |
|
|
410 |
</div> |
|
|
411 |
</div> |
|
|
412 |
|
|
|
413 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
414 |
<script>prettyPrint();</script> |
|
|
415 |
|
|
|
416 |
<script> |
|
|
417 |
YUI.Env.Tests = { |
|
|
418 |
examples: [], |
|
|
419 |
project: '../assets', |
|
|
420 |
assets: '../assets/event', |
|
|
421 |
name: 'event', |
|
|
422 |
title: 'Intro to Event Delegation', |
|
|
423 |
newWindow: '', |
|
|
424 |
auto: false |
|
|
425 |
}; |
|
|
426 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
427 |
YUI.Env.Tests.examples.push('synth-example'); |
|
|
428 |
YUI.Env.Tests.examples.push('swipe-example'); |
|
|
429 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
430 |
YUI.Env.Tests.examples.push('widget-extend'); |
|
|
431 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
432 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
433 |
YUI.Env.Tests.examples.push('get'); |
|
|
434 |
|
|
|
435 |
</script> |
|
|
436 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
437 |
|
|
|
438 |
|
|
|
439 |
|
|
|
440 |
</body> |
|
|
441 |
</html> |