Understanding Delegated JavaScript Events
While I ended up using a CSS-only implementation for this pen, I started by writing it mostly using classes and JavaScript.
However, I had a conflict. I wanted to use Delegated Events but I also wanted to minimize the dependancies I wanted to inject. I didn't want to have to import all of jQuery for this little test, just to be able to use delegated events one bit.
Let's take a closer look at what exactly delegated events are, how they work, and various ways to implement them.
Ok, so what's the issue?
Let's look at a simplified example:
Let's say that I had a list of buttons and each time I clicked on one, then I want to mark that button as "active". If I click it again, then deactivate it.
So let's start with some HTML:
<ul class="toolbar">
<li><button class="btn">Pencil</button></li>
<li><button class="btn">Pen</button></li>
<li><button class="btn">Eraser</button></li>
</ul>
I could use standard JavaScript event handler by doing something like this:
var buttons = document.querySelectorAll(".toolbar .btn");
for(var i = 0; i < buttons.length; i++) {
var button = buttons[i];
button.addEventListener("click", function() {
if(!button.classList.contains("active"))
button.classList.add("active");
else
button.classList.remove("active");
});
}
And this looks good... but it wont' work... Not the way one might expect it to.
Bitten by closures
For those of you that have been doing functional JavaScript for a while, the problem is pretty obvious.
For the uninitiated, the handler function closes over the button variable. However, there is only one of them; it gets reassigned by each iteration of the loop.
The first time though the loop, it points a the first button. The next time, the second button. And so on. However, by the time that you click one of the elements, the loop has completed and the button variable will point at the last element iterated over. Not good.
What we really need is a stable scope for each function; let's refactor an extract a handler generator to give us a stable scope:
var buttons = document.querySelectorAll(".toolbar button");
var createToolbarButtonHandler = function(button) {
return function() {
if(!button.classList.contains("active"))
button.classList.add("active");
else
button.classList.remove("active");
};
};
for(var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", createToolBarButtonHandler(buttons[i]));
}
Better! And now it actually works. We are using a function to create us a stable scope forbutton, so the button in the handler will always point at the element that we think it will.
So, what the problem?
This seems good and it will work for the most part. However, we can still do better.
First, we are making a lot of handlers. For each element that matches .toolbar buttonwe create a function and attach it as an event listener. With the three buttons we have right now the allocations are negligible.
However, what if we had:
<ul class="toolbar">
<li><button id="button_0001">Foo</button></li>
<li><button id="button_0002">Bar</button></li>
// ... 997 more elements ...
<li><button id="button_1000">baz</button></li>
</ul>
It won't blow up, but it is far from ideal. We are allocating a bunch of function that we don't have to. Let's try to refactor so that we can share a single function that is attachedmultiple times.
Rather than closing over the button variable to keep track of which button we clicked on, we can use event object that is handed to each event handler as the first argument.
The event object contains some metadata about the event. In this case, we want the thecurrentTarget property of the event to get a reference to the element that was actually clicked on.
var buttons = document.querySelectorAll(".toolbar button");
var toolbarButtonHandler = function(e) {
var button = e.currentTarget;
if(!button.classList.contains("active"))
button.classList.add("active");
else
button.classList.remove("active");
};
for(var i = 0; i < buttons.length; i++) {
button.addEventListener("click", toolbarButtonHandler);
}
Great! This not only simplified down to a single function that is added multiple times, also made the code more readable by factoring out our generator function.
But, we can still do better.
Let's say we added some buttons dynamically into the list. Then we would also need to remember to wire up the event listeners directly to those dynamic elements. And we would have to hold onto a reference to that handler and reference from more places. That doesn't sound like fun.
Perhaps there is a different approach.
Let's start by getting a better understanding of how events work and how they move through the DOM.
Okay, how do (most) events work?
When the user clicks on an element, an event gets generated to notify the application of the user's intent. Events get dispatched in three phases:
- Capturing
- Target
- Bubbling
NOTE: Not all events bubble/capture, instead they are dispatched directly on the target, but most do.
The event starts outside the document and then descends though the DOM hierarchy to the target of the event. Once the event reaches it's target, it then turns around and heads back out the same way, until it exits the DOM.
Here is a full HTML example:
<html>
<body>
<ul>
<li id="li_1"><button id="button_1">Button A</button></li>
<li id="li_2"><button id="button_2">Button B</button></li>
<li id="li_3"><button id="button_3">Button C</button></li>
</ul>
</body>
</html>
If the user clicks on Button A, then the event would travel like this like this:
START
| #document \
| HTML |
| BODY } CAPTURE PHASE
| UL |
| LI#li_1 /
| BUTTON <-- TARGET PHASE
| LI#li_1 \
| UL |
| BODY } BUBBLING PHASE
| HTML |
v #document /
END
Notice that you can follow the path the event takes down to the element that gets clicked on. For any button we click on in our DOM, we can be sure that the event will bubble back out through our parent ul element. We can exploit this feature of the event dispatcher, combined with our defined hierarchy to simplify our implementation and implement Delegated Events.
Delegated Events
Delegated events are events that are attached to a parent element, but only get executed when the target of the event matches some criteria.
Let's look at a concrete example and switch back to our toolbar example DOM from before:
<ul class="toolbar">
<li><button class="btn">Pencil</button></li>
<li><button class="btn">Pen</button></li>
<li><button class="btn">Eraser</button></li>
</ul>
So, since we know that any clicks on the button elements will get bubbled through theUL.toolbar element, let's put the event handler there instead. We'll have to adjust our handler a little bit from before;
var toolbar = document.querySelector(".toolbar");
toolbar.addEventListener("click", function(e) {
var button = e.target;
if(!button.classList.contains("active"))
button.classList.add("active");
else
button.classList.remove("active");
});
That cleaned up a lot of code, and we have no more loops! Notice that we use e.targetinstead of e.currentTarget as we did before. That is because we are listening for the event at a different level.
e.targetis actual target of the event. Where the event is trying to get to, or where it came from, in the DOM.e.currentTargetis the current element that is handling the event.
In our case e.currentTarget will be the UL.toolbar.
More Robust Delegated Events
Right now, we handle any click on any element that bubbles though UL.toolbar, but our matching strategy is a little too simple. What if we had more complicated DOM that included icons and items that were supposed to be non-clickable
<ul class="toolbar">
<li><button class="btn"><i class="fa fa-pencil"></i> Pencil</button></li>
<li><button class="btn"><i class="fa fa-paint-brush"></i> Pen</button></li>
<li class="separator"></li>
<li><button class="btn"><i class="fa fa-eraser"></i> Eraser</button></li>
</ul>
OOPS. Now, when we click on the LI.separator or the icons, we add the active class to that element. That's not cool. We need a way to filter our events so we only react to elements we care about, or if our target element is contained by an element we care about.
Let's make a little helper to handle that:
var delegate = function(criteria, listener) {
return function(e) {
var el = e.target;
do {
if (!criteria(el)) continue;
e.delegateTarget = el;
listener.apply(this, arguments);
return;
} while( (el = el.parentNode) );
};
};
This helper does two things, first it walks though each element and their parents to see if it matches a criteria function. If it does, then it adds a property to the event object calleddelegateTarget, which is the element that matched our filtering criteria. And then invokes the listener. If nothing matches, the no handlers are fired.
We can use it like this:
var toolbar = document.querySelector(".toolbar");
var buttonsFilter = function(elem) { return elem.classList && elem.classList.contains("btn"); };
var buttonHandler = function(e) {
var button = e.delegateTarget;
if(!button.classList.contains("active"))
button.classList.add("active");
else
button.classList.remove("active");
};
toolbar.addEventListener("click", delegate(buttonsFilter, buttonHandler));
BOOM! That's what I'm talking about: A single event handler, attached to a single element that does all the work, but only does it on the elements that we care about and will react nicely to elements added or removed from the DOM dynamically.
Wrapping up
We've looked at the basics of how to implement event delegation in pure javascript in order to reduce the number of event handlers we need to generate or attach.
There are a few things I would do, if I were going to abstract this into a library, or use it for production level code:
- Create helper functions to handle criteria matching in a unified functional way. Something like:
var criteria = {
isElement: function(e) { return e instanceof HTMLElement; },
hasClass: function(cls) {
return function(e) {
return criteria.isElement(e) && e.classList.contains(cls);
}
}
// More criteria matchers
};
- A partial application helper would also be nice:
var partialDelgate = function(criteria) {
return function(handler) {
return delgate(criteria, handler);
}
};
If you have any suggestions or improvments, drop a comment or send me a message! Happy coding!
http://codepen.io/32bitkid/blog/understanding-delegated-javascript-events
Understanding Delegated JavaScript Events的更多相关文章
- Understanding the JavaScript Engine—— two phase
Understanding the JavaScript Engine — Part 1 I have been a Ruby on Rails developer for the last 2 ...
- Javascript Events
事件通常与函数配合使用,这样就可以通过发生的事件来驱动函数执行. 事件句柄 html4.0的新特性之一是有能力使html事件触发浏览器中的动作action,比如当用户点击某个html元素时启动一段Ja ...
- Javascript Madness: Mouse Events
http://unixpapa.com/js/mouse.html Javascript Madness: Mouse Events Jan WolterAug 12, 2011 Note: I ha ...
- JavaScript Interview Questions: Event Delegation and This
David Posin helps you land that next programming position by understanding important JavaScript fund ...
- 推荐15款制作 SVG 动画的 JavaScript 库
在当今时代,SVG是最流行的和正在被众多的设计人员和开发人员使用,创建支持视网膜和响应式的网页设计.绘制SVG不是一个艰巨的任务,因为大量的 JavaScript 库可与 SVG 图像搭配使用.这些J ...
- 常用的Javascript设计模式
<parctical common lisp>的作者曾说,如果你需要一种模式,那一定是哪里出了问题.他所说的问题是指因为语言的天生缺陷,不得不去寻求和总结一种通用的解决方案. 不管是弱类型 ...
- Javascript——浅谈 Event Flow
1.Javascript Events : Event Bubbling(事件冒泡) 如果事件从最特定的元素开始,则事件流中的一个阶段称为事件冒泡(DOM中可能最深的节点)然后向上流向最不特定的节点( ...
- 集成Javascript Logging on MVC or Core
ASP.NET Core provides us a rich Logging APIs which have a set of logger providers including: Console ...
- 每个JavaScript工程师都应懂的33个概念
摘要: 基础很重要啊! 原文:33 concepts every JavaScript developer should know 译文:每个 JavaScript 工程师都应懂的33个概念 作者:s ...
随机推荐
- iotop使用详解
iotop是top和iostat程序的混合体,能够显示系统中所有运行进程并将进程根据I/O统计信息排序. 这个软件使用了Linux内核的一些新特性,所以需要2.6.20或者更新的内核. 一般默认情况下 ...
- 【pbrt】在c++程序中使用pbrt进行渲染
近段时间做一个关于水面的动画.由于我用c++实现水面动画的,然而使用c++我自己的渲染系统渲染结果被同学说是可视化不叫渲染,所以我决定修改一下…… 恰好进来在学习pbrt,所以索性就蛋疼了考虑直接用p ...
- ADB命令详解及大全( 声明:此文是参考大佬博客所做的笔记!)
adb是什么? adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.a ...
- (转)C++ 自由存储区是否等价于堆?
“free store” VS “heap” 当我问你C++的内存布局时,你大概会回答: “在C++中,内存区分为5个区,分别是堆.栈.自由存储区.全局/静态存储区.常量存储区”. 如果我接着问你 ...
- 使用Fragment填充ViewPager
在上一篇文章中,讲解了使用PagerAdapter作为适配器时的ViewPager的使用方法.然后在实际项目中更多的使用Fragment作为页卡,因为实际开发中每一个页卡要复杂的多.而使用Fragme ...
- 用eclipse pydev 创建一个新py文件时 文件的coding设置问题
问题: 当安装好eclipse和pydev后,创建一个project, 创建一个新的py文件,文件头都会自带中文时间.这样在编译的时候会报错. 解决办法之一: 通过设置,可以使新建的文件的文件头自动带 ...
- PHP中__get()和__set()的用法实例详
刚刚看到一个对我有用的文章,我就把它摘抄下来了. php面 ...
- maven学习利用Profile构建不同环境的部署包
项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre).正式生产环 ...
- 使用Scanner将InputStream类型转换成String
我们在测试项目中经常会遇到这样的情形: 1. 从文件或网络得到一个InputStream,需要转换成String赋值到别的变量做为另一个方法的入参. 2. 从文件或网络得到一个InputStream后 ...
- GIT 报错:Result too large 解决办法
在使用bower install命令下载前端依赖的js插件时,git出错了,报错信息如下: bower ECMDERR Failed to execute "git ls-remote -- ...