Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

How to properly update JS after editing a section in Customizer

How to properly update JS after editing a section in Customizer

Rostislav333
Shopify Partner
10 0 0

I have a Shopify section with a SplideJS slider. The problem was a buggy update of the slider in the customizer. I found a way to update correctly in the customizer and the method works. But the problem is that I am duplicating the code for this. Is there any other way to avoid this? Maybe I'm doing it wrong

document.addEventListener('DOMContentLoaded', () => {
    const rlHotspotSlider = new Splide('.rl-hotspot-slider', {
      pagination: false,
      gap: 1,
      drag: false,
      perPage: 6,
      perMove: 1,
    });
    rlHotspotSlider.mount();

    if (Shopify.designMode) {
      document.addEventListener('shopify:section:load', function(event) {
        const rlHotspotSlider = new Splide('.rl-hotspot-slider', {
          pagination: false,
          gap: 1,
          drag: false,
          perPage: 6,
          perMove: 1,
        });
        rlHotspotSlider.mount();
      });
    }
  });
Reply 1 (1)

shoppie-user-12
Shopify Partner
1 0 0

@Rostislav333 Thanks for this solution, I was having the same issue. Regarding your question I just created a function out of it:

 

document.addEventListener( 'DOMContentLoaded', function() {
    initSplide();

    if (Shopify.designMode) {
      document.addEventListener('shopify:section:load', function(event) {
        initSplide();
      });
    }
});

function initSplide() {
  const splides = document.querySelectorAll('.splide');
  // do your initialisation here
}