Comments (10)
Here is an modified example of one of your examples to show this issue, if you look at the console, you'll see its called twice and not batched together.
https://codepen.io/mouse0270/pen/LYJdJLB?editors=1111
from reef.
@mouse0270 (responding to the edit)
https://reefjs.com/api/#lifecycle-events
https://reefjs.com/advanced/#batch-rendering
document.addEventListener('reef:render', function (event) {
console.log('The UI was just updated inside this element.');
console.log(event.target);
if (event.target && event.target.closest('.myElement')) {
}
});
Let me know if this doesn't answer your question. One method to achieve your desired outcome is to leverage event propagation to watch for a specific component being rendered.
from reef.
@mouse0270 (responding to the edit)
https://reefjs.com/api/#lifecycle-events https://reefjs.com/advanced/#batch-rendering
document.addEventListener('reef:render', function (event) { console.log('The UI was just updated inside this element.'); console.log(event.target); if (event.target && event.target.closest('.myElement')) { } });
Let me know if this doesn't answer your question. One method to achieve your desired outcome is to leverage event propagation to watch for a specific component being rendered.
the problem with reef:render
is that it triggers when any component renders, I wanted an event similar to how store works
document.addEventListener('reef:render-Action', async (event) => { ... });
I basically solved this issue by doing
// Refresh Select Pickers when Reactive Components are Rendered
document.addEventListener('reef:render', function (event) {
// Create Control Specific Render Functions
const id = event.target.id.replace('component', '');
event.target.dispatchEvent(new CustomEvent(`reef:render-${id}`, {
bubbles: true,
detail: CONTROLS[id]
}));
});
But this really only works because of how I setup my components.
from reef.
@mouse0270 Forgive me, as I haven't had a full cup of coffee yet today (☕️), but if I understand what you're saying correctly...
The reef.store()
method fires multiple times when several pieces of data contained it are updated, not that reef.render()
renders the UI multiple times in response, correct?
from reef.
Yes, so for example document.addEventListener('reef:store-cart', function () { ... });
would fire on each update to the store.
cart.push('shirt');
cart.push('pants');
This would cause the conde inside of reef:store-cart
event to be fired twice, once for each push. However... document.addEventListener('reef:render', function (event) { ... });
only gets fired once. But the problem is that reef:render
has no way to attach itself to a specific component. It gets called when any component renders.
I think reef:render
is what I am looking for, as I want to run code once the component is done updating and not once the store is updated. But reef:render
doesn't currently have component specific event listeners. Which in my example, you can see I sorta of got around, but it would be nice to have an official way to bind an event listener when a specific component renders.
from reef.
Basically I think this boils down to, I'd like to make a feature request, so that we can have an official event listener for specific components.... so basically we could do something like this
let {store, component} = reef;
// Use a custom event name
let wizards = store([], 'wizards');
let mages = store([], 'mages');
// Create a template
function template () {
return `
<ul>
${wizards.map(function (wizard) {
return `<li>${wizard}</li>`;
}).join('')}
</ul>`;
}
// Create a template
function template2 () {
return `
<ul>
${mages.map(function (mage) {
return `<li>${mage}</li>`;
}).join('')}
</ul>`;
}
component('#app', template, {stores: ['wizards']});
// Use a custom name AND allow on* events
component('#app2', template2, {stores: ['mages'], events: true});
// Log whenever an element is rendered into
document.addEventListener('reef:render', function (event) {
console.log('The UI was just updated a component.');
console.log(event.target);
});
// Log whenever an element is rendered into
document.addEventListener('reef:render-app', function (event) {
console.log('The UI was just for the #app component.');
console.log(event.target);
});
// Log whenever an element is rendered into
document.addEventListener('reef:render-app2', function (event) {
console.log('The UI was just for the #app2 component.');
console.log(event.target);
});
from reef.
@mouse0270 Thanks, that clarifies things!
So yes, the rendering is batched, not the updating. You'll want to listen for reef:render
events. The event is actually emitted on the parent element that the component renders into, so you can narrow what you listen to that way.
Using your example above, you can do this today...
// Log whenever an element is rendered into
document.querySelector('#app').addEventListener('reef:render-app', function (event) {
console.log('The UI was just for the #app component.');
console.log(event.target);
});
// Log whenever an element is rendered into
document.querySelector('#app2').addEventListener('reef:render-app2', function (event) {
console.log('The UI was just for the #app2 component.');
console.log(event.target);
});
You could also do this...
// Log whenever an element is rendered into
document.addEventListener('reef:render-app', function (event) {
if (event.target.matches('#app')) {
console.log('The UI was just for the #app component.');
}
if (event.target.matches('#app2') {
console.log('The UI was just for the #app2 component.');
}
console.log(event.target);
});
Hope that helps!
from reef.
Awe. I didn't realize the event was rendered on the component. I believe you mean as reef:render-app
isn't valid.
// Log whenever an element is rendered into
document.querySelector('#app').addEventListener('reef:render', function (event) {
console.log('The UI was just for the #app component.');
console.log(event.target);
});
// Log whenever an element is rendered into
document.querySelector('#app2').addEventListener('reef:render', function (event) {
console.log('The UI was just for the #app2 component.');
console.log(event.target);
});
That also works just fine, is it stated anywhere in the docs that the reef:render
is triggered on the component? Did I just miss this? I really thought I read though the documents fairly well. but its a small thing I could be missing that.
from reef.
Yea, it says it here: https://reefjs.com/api/#lifecycle-events
reef:render
is emitted on a component element when reef renders a UI update
from reef.
Yup, just something small that I missed. Well call this closed. haha
from reef.
Related Issues (20)
- 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
- Working with Events Questions HOT 2
- 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
- Lack of event support in component HOT 2
- Uncaught TypeError HOT 2
- License? HOT 2
- Add support for on* events that does so safely HOT 2
- 12.3 has broken my app HOT 2
- support disabled and readonly attributes with @ syntax? HOT 5
- jsdoc issue 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.