Hi,
I have a question about turbo framework.
I init a carousel like below:
function initHomepageCarousel() {
$(".yuppiCarousel").slick({
lazyLoad: "ondemand",
autoplay: true,
dots: false,
arrows: true,
infinite: true,
speed: 1000,
slidesToShow: 1,
slidesToScroll: 1,
prevArrow: ".yuppiCarouselLeftArrow",
nextArrow: ".yuppiCarouselRightArrow",
});
}
$(document).on("render", function () {
initHomepageCarousel();
});
But when I naviage to the specific page where carousel is, a I got the following error in browser’s console. So does not init the carousel. After five times navigation I have this error 23 times.
I followed the code like in the Demo theme’s theme-leader.js
I really aprecciate if anyone could help me
Thanks,
Zs
daft
July 13, 2023, 8:08am
#2
Try using hot controls
Here is an example:
oc.registerControl('yuppi-carousel', class extends oc.ControlBase {
connect() {
this.$el = $(this.element);
this.connectSlick();
}
disconnect() {
this.$el.slick('unslick');
this.$el = null;
}
connectSlick() {
this.$el.slick({
lazyLoad: "ondemand",
autoplay: true,
dots: false,
arrows: true,
infinite: true,
speed: 1000,
slidesToShow: 1,
slidesToScroll: 1,
prevArrow: ".yuppiCarouselLeftArrow",
nextArrow: ".yuppiCarouselRightArrow",
});
}
});
Then render it with:
<div data-control="yuppi-carousel"> ... </div>
I hope this helps!
@daft what could be the probleme?
Why runs a lot of time the js codes?
oc.registerControl(
"yuppi-carousel",
class extends oc.ControlBase {
connect() {
this.$el = $(this.element);
this.connectSlick();
console.log("slick yuppi-carousel");
}
disconnect() {
this.$el.slick("unslick");
this.$el = null;
console.log("unslick yuppi-carousel");
}
connectSlick() {
this.$el.slick({
lazyLoad: "ondemand",
autoplay: true,
dots: false,
arrows: true,
infinite: true,
speed: 1000,
slidesToShow: 1,
slidesToScroll: 1,
prevArrow: ".yuppiCarouselLeftArrow",
nextArrow: ".yuppiCarouselRightArrow",
});
}
}
);
daft
July 20, 2023, 4:55am
#4
This happens because of the turbo snapshot. You can disable it if you like, but in most cases its not really a problem.
When you click a link that has been visited before:
A cached snapshot is rendered
Hot controls will initialize (1)
The page is requested
The latest page is rendered
Hot controls will initialize (2)