Comments (2)
Hi @mouse0270, so... it's all about scope.
The Class needs to be in the global scope to call it form an on*
event on the element. As an alternative to on
events, you can use event delegation without having to wait for the render to happen.
If you are going to attach events directly to the rendered elements, I would use reef:start
as your event hook if they're not going to be removed from the UI later.
from reef.
Its a module, it doesn't get defined on the global scope. Which is fine, I was just trying to provide an example of why passing in events used to be a very nice feature.
Also, event delegation is great, and you did a fantastic job writing that article, it might be best to update it. Though it does a great job at explaining how to use event delegation in pure vanilla JS, it leaves out the fact that it won't work on any element that has children in it.
<button class="click-me" id="click-me-1">Click Me</button>
<button class="click-me" id="click-me-2"><span>Click Me</span></button>
<script>
// Listen for click events
document.addEventListener('click', function (event) {
if (!(event.target.matches('.click-me'))) return;
console.log('An element was brought into focus.');
console.log(event.target);
});
</script>
In this example, you'll find that the second button never triggers the click
event unless you click on the very edge of the button because javascript has registered that the span
is the target element.
changing the script to something like
// Listen for click events
document.addEventListener('click', function (event) {
if (!(event.target.closest('.click-me'))) return;
const element = event.target.closest('.click-me')
console.log('An element was brought into focus.');
console.log(element );
});
I know this was off-topic, but it was a really well written article and figured it would be a nice addition to include some of the limitations of event delegations and how to get around them.
from reef.
Related Issues (20)
- Input fields not updated to reflect data changes HOT 3
- Issues with listeners and multiple on events HOT 1
- "Ready" event never fires HOT 1
- Binding input value changes to data HOT 1
- Objects with functions lost when copied to props HOT 1
- "on" attributes not being properly converted to event listeners
- Obj events = {} storing duplicate event listiners causing duplicate calls HOT 3
- Add component.destroy() method HOT 3
- Add Source Map Files HOT 1
- Nested Components + events? HOT 1
- Support key for memoization HOT 3
- Use domdiff? HOT 3
- Events for component HOT 5
- Using Map or Set as store HOT 2
- Is there a way to force a component to "rerender" without adding content? HOT 3
- Lifecycle Events - reef:render is fired on all components in the document HOT 2
- Can I use Reef to generate the HTML for a given route? HOT 1
- Updating Multiple Properties at Once HOT 10
- Lack of event support in component HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reef.