I have created a basic app that displays images and allows users to add more. When adding an image, I show a spinner while it loads and hide the spinner once the loading is complete. However, I encountered a problem where the spinner does not appear when adding a second image. This issue seems to be caused by the boolean variable I am using, which appears to be used globally.
It's important to note that I add images using their URL and render them with the HTML img tag. My app utilizes RactiveJS, and I believe there might be a solution involving a unique identifier, although I'm unsure of how to implement it. Each image has a unique ID assigned to its data.
Below is the snippet of Ractive code:
let isImageLoaded = false;
const ractive = new Ractive({
el: '#container',
template: `
{{#each images}}
<div class="container">
{{#if !isImageLoaded}}
<span class="spinner"></span>
{{/if}}
<img
alt=""
src="{{url}}"
on-load="imageLoaded"
style="display: {{isImageLoaded ? 'block' : 'none'}};"
/>
</div>
{{/each}}
`,
data: {
images: [
{ id: 1, url: 'https://www.some-image.com/image1.jpg' },
],
isImageLoaded : isImageLoaded
}
});
ractive.on('imageLoaded', function(e) {
ractive.set('isImageLoaded', true);
});
function addImage(image) {
const allImages = ractive.get('images');
allImages.push(images);
ractive.set('isImageLoaded', false);
ractive.set('images', allImages);
}
If I set isImageLoaded
to false in the addImage
function, adding a new image causes all other spinners to appear.
I am seeking advice on how to use IDs to make each image and spinner unique, ensuring the spinner only appears when adding a new image. Any suggestions would be greatly appreciated!