Meteor with iron-router cant get slick carousel working

旧城冷巷雨未停 提交于 2019-12-17 16:55:26

问题


I am using iron-router, i have a route "models" which has a list of items. When the user clicks one of these items a new route "model" is used, the name of the selected item is passed as a param and used to load data about that model from the database.

I want to use slick carousel, using an array of images returned from the database (based on the param).

<div id="carousel">
    {{#each images}}
    <div class="item">
        <img src="/images/{{this}}" alt="">
    </div>
    {{/each}}
</div>

Where should I call the slick init?


回答1:


Generally speaking, you should initialize plugins in a template's onRendered callback. In your case, that won't work because onRendered will fire before any of the images are inserted into the DOM. With other carousel plugins I've seen, the following strategy works:

  1. Move each item to its own template (carouselItem).
  2. Add an onRendered callback to carouselItem so that the plugin will be initialized after each item is added to the DOM.

I haven't tried this with slick 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 slick carousel can be initialized multiple times, this approach should work. Be aware that one possible downside is that the plugin will refresh whenever images is updated. One way to fix that is to use the {reactive: false} option in your find.




回答2:


Usually you would call a plugin on an element in Template.myTemplate.onRendered:

Template.xxx.onRendered(function() {
  $('#carousel').slick();
});`


来源:https://stackoverflow.com/questions/30140232/meteor-with-iron-router-cant-get-slick-carousel-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!