【问题标题】:Meteor - Isotope doesn’t work with nested templatesMeteor - Isotope 不适用于嵌套模板
【发布时间】:2017-09-29 03:42:09
【问题描述】:

我正在尝试将 Isotope 与我的 Meteor 应用程序集成,但我遇到了一些问题。当我在 Isotope 容器中直接使用 html 时,它可以正常工作,但是当我尝试使用嵌套模板时,Isotope 不起作用。这是我目前拥有的。

Home.html

<template name="home">
  <div class="grid">
    {{> card }}
  </div>
</template>

Home.js

import './home.html';

Template.home.onRendered ( function() {

      $('.grid').isotope({
        // options
        itemSelector: '.grid-item',
        layoutMode: 'masonry',
        masonry: {
          gutter: 24
        }
      });
 });

Card.html

<template name="card">

  {{#each publications}}
    <div class="grid-item">
      <div class="card-margin">
        <img src="{{avatar}}" alt="" class="avatar"/>
        <div class="name"> {{username}} <div class="dropdown"><a>></a><div class="dropdown-content">{{#if isOwner}}<button class="delete">&times;</button> <button class="edit">Edit</button>{{else}}<button>Report</button> <button>Unfollow</button>{{/if}}</div></div></div>
        <div class="date" id="date-show">{{customDate}} </div>
        <p class="card">
            {{ pub }}
        </p>
      </div>
    </div>
  {{/each}}
</template>

任何帮助都会很棒。

【问题讨论】:

  • 可能home 是在发布数据可用之前呈现的,在这种情况下可能还没有任何.grid-items 准备好?您是否尝试将砌体代码改为 card.onRendered()
  • 我已经更新了我的答案,也许另一个选项更适合你。
  • 谢谢迈克尔,我已经尝试了 Styx 的答案并且它有效。你是对的,我的嵌套模板是在我的主模板之后加载的,所以 Isotope 没有找到我的网格项。

标签: javascript meteor meteor-blaze jquery-isotope


【解决方案1】:

问题本身是您的home 模板在其嵌套模板之前呈现。因此,没有任何.grid-item 元素。

在您的情况下解决此问题的唯一方法是推迟使用 Meteor.defer()Meteor.setTimeout() 初始化 .isotope(...),希望这会给嵌套模板足够的时间来呈现:

Template.home.onRendered(function() {
  Meteor.setTimeout(function() {
    $('.grid').isotope({
      // options
    });
  }, 250);

添加

另一个选项是嵌套模板通过 onRendered 函数通知其父级:

Main template

Template.home.onCreated(function() {
  this.renderGrid = _.debounce(() => {
    $('.grid').isotope({
      // options
    });
  }, 250);
});

Template.home.events({
  'ready.griditem .grid'(e, tpl) {
    tpl.renderGrid();
  }
});

Nested template

Template.card.onRendered(function() {
  $('.grid').trigger('ready.griditem');
});

【讨论】:

  • 有效!感谢 Styx,将时间设置为 200 可以完美运行。
  • 我真的更喜欢使用 onRender 而不是超时。使用超时可能会降低 UI/ 的响应能力
  • @Alex 好吧,我认为isotope 应该同时初始化所有嵌套模板。他们不能都在onRendered 中包含.isotope()
猜你喜欢
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
  • 2016-06-24
  • 1970-01-01
  • 2014-12-24
  • 2014-11-22
  • 2023-03-11
相关资源
最近更新 更多