Generally speaking, you should initialize the plugins in the onRendered callback . In your case, this will not work, because onRendered will fire before any image is inserted into the DOM. The following strategy works with the other carousel plugins I've seen:
- Move each item to its own template (
carouselItem ). - Add the
onRendered to carouselItem so that the plugin is initialized after each element is added to the DOM.
I have not tried this with a smooth carousel, but it would look something like this:
<template name="carousel"> <div id="carousel"> {{#each images}} {{> carouselItem}} {{/each}} </div> </template> <template name="carouselItem"> <div class="item"> <img src="/images/{{this}}"> </div> </template>
Template.carouselItem.onRendered(function() { $('#carousel').slick(); });
Assuming that the spotted carousel can be initialized several times, this approach should work. Keep in mind that one of the possible disadvantages is that the plugin will be updated when the images updated. One way to fix this is to use the {reactive: false} parameter in find .
source share