简体   繁体   English

Shopify ThemeEditor 在编辑后不执行 Javascript 导致部分重新加载

[英]Shopify ThemeEditor doesnt execute Javascript after edit caused Section reload

I wrote a custom Shopify section.我写了一个自定义 Shopify 部分。 The section contains a slider, and each block added to the section results in a new slide added to the slider.该部分包含一个滑块,添加到该部分的每个块都会导致添加到滑块的新幻灯片。

When editing slides (blocks), the slider should automatically slide to the selected block for proper preview experience.编辑幻灯片(块)时,滑块应自动滑动到选定的块以获得正确的预览体验。 I wrote a js function for that, that works perfectly fine:我为此编写了一个 js 函数,效果很好:

if (Shopify.designMode) {
  document.addEventListener('shopify:block:select', function(event) {
    const sectionSelectedIsHero = event.target.classList.contains('hero-slide');
    if(!sectionSelectedIsHero) return;
    
    var selectedSlideId = $(event.target).attr("data-slide-id");

    heroCarousel.to(selectedSlideId);
  });
} 

However, when a user edits a slide (block), for example the background color in a color picker, Shopify automatically reloads the section in the ThemeEditor to update the preview for the user.但是,当用户编辑幻灯片(块)时,例如颜色选择器中的背景颜色,Shopify 会自动重新加载 ThemeEditor 中的部分以更新用户的预览。

While the shopify:block:select event is being fired again (I can track "event" variable in console.log()), the slider doesnt work at all after that.虽然 shopify:block:select 事件再次被触发(我可以在 console.log() 中跟踪“事件”变量),但此后滑块根本不起作用。 The first slide is in preview and the controls also stop working.第一张幻灯片处于预览状态,控件也停止工作。 When selecting different slides (blocks), nothing happens.选择不同的幻灯片(块)时,没有任何反应。

Appreciate any help.感谢任何帮助。 Thanks!谢谢!

EDIT: I now have a working solution.编辑:我现在有一个可行的解决方案。 However, this is not optimal at all as im repeating code just to make it execute again.然而,这根本不是最优的,因为我重复代码只是为了让它再次执行。 How can I avoid that?我怎样才能避免这种情况? Does anyone have a suggestion?有人有建议吗?

$( document ).ready(function() {
var heroCarouselElement = document.querySelector('#heroCarousel');
var heroCarousel = new bootstrap.Carousel(heroCarouselElement, {
  interval: false,
  keyboard: false,
  wrap: true,
  touch: true
});

$("#heroControlPrev").click(function() { heroCarousel.prev(); });
$("#heroControlNext").click(function() { heroCarousel.next(); });


if (Shopify.designMode) {
  document.addEventListener('shopify:block:select', function(event) {
    const sectionSelectedIsHero = event.target.classList.contains('hero-slide');
    if(!sectionSelectedIsHero) return;

    var selectedSlideId = $(event.target).attr("data-slide-id");
    var heroCarouselElement = document.querySelector('#heroCarousel');
    var heroCarousel = new bootstrap.Carousel(heroCarouselElement, {
      interval: false,
      keyboard: false,
      wrap: true,
      touch: true
    });

    $("#heroControlPrev").click(function() { heroCarousel.prev(); });
    $("#heroControlNext").click(function() { heroCarousel.next(); });

    heroCarousel.to(selectedSlideId);
  });
} 

}); });

On my mind, the issue is about the event.在我看来,问题是关于事件的。

You might try this:你可以试试这个:

if (Shopify.designMode) {
  document.addEventListener('shopify:section:load', function(event) {
    /* Do something */
  });
}

As explained in doc the load event is fired when "A section has been added or re-rendered."文档中所述,当“添加或重新渲染了一个部分”时会触发加载事件。 Which is the case each time user add a slide (re-rendering).每次用户添加幻灯片(重新渲染)时都会出现这种情况。

Please not that you might have to reset/clear the function initializing the slider before, using the unload event (depending the way the slider works).请注意,您可能必须使用 unload 事件(取决于滑块的工作方式)重置/清除之前初始化滑块的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM