-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
change code to use .addEventListener
instead?
#103
Comments
We don't handle or keep track of events. The ability to add events is just a by-product of being able to set properties through a bel tagged template string, in this case ones that prefer to be set directly on the object instead. Also It would be more difficult to support const el = bel`<button>click</button>`
el.addEventListener('click', function () {}, false); |
That article is really great actually, its a glaring solution to passing context without binding, thus maintaining the prototype chain advantages (vs of 100s of bound instance functions). The big 'secret' to it that adding the same event handler multiple times automatically de-dupes attachments somehow... so even adding multiple listeners might not be an issue, since you really are in-effect adding one. Not sure if it scales to 100s of calls or not, but it might! The complications comes from the fact you need to pass in an object with the |
What about testing and using |
More specifically, my initial idea was use addEventListener if the handler implements |
I tried to explore it. I never tried that before. To me it looks, that when you discard a
This was the code I ran and recorded for 15 seconds I'm not sure if this is actually measuring what you are interested in. It's the first time I try to do this. <html>
<head>
<title> test </title>
</head>
<body>
<script>
var n = 1000
var str = 'foobarbaz'.repeat(n)
var bigarray = Array.apply(null, Array(n))
var filledarray = bigarray.map(String.prototype.valueOf,str)
var json = JSON.stringify(filledarray)
function makeNode () {
var button = document.createElement('button')
button.innerText = 'foobarbaz'
// var fn = function listener (event) { console.log(event) }
// fn.somethingbig = JSON.parse(json)
// button.addEventListener('click', fn)
// var fn = function listener (event) { console.log(event) }
// fn.somethingbig = JSON.parse(json)
// button.onclick = fnBound
var obj = { handleEvent (event) { console.log(event) } }
obj.handleEvent.somethingbig = JSON.parse(json)
button.addEventListener('click', obj)
return button
}
var counter = 10
setTimeout(function makeManyNodes () {
console.log(counter)
if (!counter) return // document.body.innerHTML = ''
counter--
var el = makeNode()
document.body.innerHTML = ''
document.body.appendChild(el)
setTimeout(makeManyNodes, 1000)
}, 1000)
</script>
</body>
</html> Below are my measurements in a chrome incognito tab button.onclick = fnbutton.addEventListener('click', fn)button.addEventListener('click', { handleEvent }) |
I saw that <html>
<head>
<title> test </title>
</head>
<body>
<script>
var n = 1000
var str = 'foobarbaz'.repeat(n)
var bigarray = Array.apply(null, Array(n))
var filledarray = bigarray.map(String.prototype.valueOf,str)
var json = JSON.stringify(filledarray)
var obj = { handleEvent (event) { console.log(event) } }
obj.handleEvent.somethingbig = JSON.parse(json)
function makeNode () {
var button = document.createElement('button')
button.innerText = 'foobarbaz'
// var fn = function listener (event) { console.log(event) }
// fn.somethingbig = JSON.parse(json)
// button.addEventListener('click', fn)
// var fn = function listener (event) { console.log(event) }
// fn.somethingbig = JSON.parse(json)
// button.onclick = fn
button.addEventListener('click', obj)
button.addEventListener('click', obj)
button.addEventListener('click', obj)
button.addEventListener('click', obj)
button.addEventListener('click', obj)
button.addEventListener('click', obj)
button.addEventListener('click', obj)
button.addEventListener('click', obj)
button.addEventListener('click', obj)
return button
}
var counter = 10
setTimeout(function makeManyNodes () {
console.log(counter)
if (!counter) return // document.body.innerHTML = ''
counter--
var el = makeNode()
// document.body.innerHTML = ''
document.body.appendChild(el)
setTimeout(makeManyNodes, 1000)
}, 1000)
</script>
</body>
</html> button.onclick = fnbutton.addEventListener('click', fn)button.addEventListener('click', { handleEvent }) |
@goto-bus-stop I personally would prefer a more minimal change, because:
So i would just pass for any listener module.exports = mycomponent
module.exports.prototype = {
handleEvent (event) { this[event.target.dataset.call](event) }
foobar (event) { }
barbaz (event) { }
}
function mycomponent () {
if (!(this instanceof mycomponent)) return new mycomponent()
// ...
var el = bel`
<div class="mycomponent">
<button data-call=foobar onclick=${this}> foobar </button>
<button data-call=barbaz onclick=${this}> barbaz </button>
<div>`
// ...
} ...but of course, just passing |
When using addEventListener, one could pass an object
onclick=${{ handleEvent }}
.This article explains it in more details what potential benefits this can have:
https://medium.com/@WebReflection/dom-handleevent-a-cross-platform-standard-since-year-2000-5bf17287fd38
I guess it would need only a single change here:
https://github.com/shama/bel/blob/68b261e6892170fb972f4488ab4dfc200b125213/browser.js#L76
el[p] = val
would become
el.addEventListener(key.slice(2), val)
I can make the PR if this change would be desirable.
If there are reasons why this would not be desirable, I'd be curious to learn about the reasons too :-)
The text was updated successfully, but these errors were encountered: