Assistance Required for Dynamically Updating Canvas Components with JavaScript

10 replies · opened Oct 27, 2023

IiridesoOct 27, 2023

Dear Canvas Support Team,

I hope this message finds you well. We are currently working on a project where we utilize the Canvas theme and have encountered a challenge that we hope you can assist us with.

Our scenario involves dynamically adding new components to a webpage, such as carousels and other elements that rely heavily on JavaScript for their functionality. However, we've noticed that after these elements are inserted into the page post-initial load, they do not function as intended. The JavaScript functionalities associated with these Canvas components do not seem to apply automatically to the newly added elements.

To give a specific example, when we dynamically add a carousel to the webpage, it does not exhibit the expected behavior that is typically initiated when the page first loads.

We have attempted a method where we listen for updates and then try to reinitialize the Canvas components like so:

javascript
Copy code
// Example of our current approach
document.addEventListener("updateEvent", function() {
// Code to reinitialize Canvas components
});
Unfortunately, this approach has not yielded the desired results. We suspect there might be specific JavaScript code or a function we need to execute to properly apply Canvas's functionalities to these dynamically added elements, without having to reload the entire page.

Could you please provide guidance or share a snippet of JavaScript code that would allow us to achieve this? Essentially, we need a way to reapply or reinitialize the Canvas theme's JavaScript functionalities to new elements added to the page after the initial load.

Thank you very much for your assistance in this matter. We appreciate any help or insights you can provide.

Best regards,

SSemicolon WebSTAFFOct 29, 2023

Hello,

Thanks for your Patience!

Absolutely this possible and we have made Canvas with an API Approach with specifically these use-cases in mind. This can be achieved using 1 Line of Code:
[ch_pre type="js"]document.addEventListener('updateEvent', function() {
SEMICOLON.Core.runContainerModules(document.querySelector('.your-container'));
});[/ch_pre]

This should work fine and will target any Dynamically added modules within the Container. Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

IiridesoOct 30, 2023

Hello!

The result of our tests:
It only works on a single element at a time and only on the last element of the stack.
How we tested it:
We have a parent div identified by #editor which has dynamic children that are all refreshed at once.
When refreshed, we applied the provided code on the parent element:
SEMICOLON.Core.runContainerModules(document.querySelector("#editor"));
but only the last of the children elements is being worked on by the code.
We tested this by injecting two carousels, and only the last one was displayed properly.
We also tried to loop through each of the children elements and apply to each of them the code, but still the same result:
const dataLayouts = document.querySelectorAll("#editor [data-layout]");
dataLayouts.forEach(element => {
SEMICOLON.Core.runContainerModules(element);
});

SSemicolon WebSTAFFOct 30, 2023

Hello,

This definitely should not happen. We have checked this out and it appears to be working fine for us. The entire JS Component runs on this code. Please provide us with a Live URL so that we can check out the exact issue and provide you with more accurate assistance on this. Thanks for your Patience.

Meanwhile, do let us know if we can help you with anything else or if you find any further issues with Canvas.

IiridesoOct 30, 2023

Thanks for your suggestion, we'll do a few more tests and build a separate prototype to make sure it's not a compatibility problem with something else.

I'll let you know if we still can't do it.

IiridesoOct 31, 2023

After various in-depth tests, we've found that the problem is that this approach doesn't work with Livewire.

Can you help?

IiridesoOct 31, 2023

the bug only affects carousels.

SSemicolon WebSTAFFOct 31, 2023

Hello,

Thanks for your Kind Patience!

Consider trying this:

Use this HTML initially for the Owl Carousel when loading this externally
[ch_pre]

[/ch_pre]

Note the .customjs Class added, this doesn't initialize the JS by default. Then use this JS Code:
[ch_pre type="js"]document.addEventListener('updateEvent', function() {
let container = document.querySelector('.your-container');

container.querySelectorAll('.customjs').forEach( el => {
	el.classList.remove('customjs');
});

setTimeout(() => {
	SEMICOLON.Core.runContainerModules(container);
}, 1000);

});[/ch_pre]

This should work but we are really not sure about the integrations with Livewire as Canvas was not built with Livewire integrated out of the box. Consider trying the above code and do let us know how this is shaping up.

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

IiridesoNov 2, 2023

Hi thanks but using setTimeout with SEMICOLON.Core.runContainerModules gives the same result and we've already tried that

SSemicolon WebSTAFFNov 4, 2023

Hello,

Apologies for the Inconveniences caused!

The above codes work fine with the Current Setup as provided with the Package, so it appears that you will need to manually integrate additional functionality for Livewire based on your Usage and the JS functionality provided currently with Canvas.

Since Canvas currently does not come pre-integrated with Livewire, we won't unfortunately be able to provide you with additional support related to this issue. Thanks for your Patience.

Meanwhile, do let us know if we can help you with anything else or if you find any further issues with Canvas.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard