【问题标题】:jQuery inside each loop meteor not works每个循环流星内的jQuery不起作用
【发布时间】:2014-07-03 21:49:16
【问题描述】:

大家,

我有一个问题在另一个案例中已经发生在我身上,但我提出这个希望我们可以解决它:

jquery(在“渲染”上调用)在不是由 eachloop 生成时运行良好……为什么不能在生成的 html 中运行?

我点击了每个循环生成的图像,没有任何反应

gallery.html

    {{#each gallery}}
        <div class="superbox-list">
            <img src="images/superbox/superbox-thumb-1.jpg" data-img="images/superbox/superbox-full-1.jpg" alt="My first photoshop layer mask on a high end PSD template theme" title="Miller Cine" class="superbox-img">
        </div>
    {{/each}}
        <div class="superbox-list">
            <img src="images/superbox/superbox-thumb-1.jpg" data-img="images/superbox/superbox-full-1.jpg" alt="My first photoshop layer mask on a high end PSD template theme" title="Miller Cine" class="superbox-img">
        </div>
        <div class="superbox-list">
            <img src="images/superbox/superbox-thumb-2.jpg" data-img="images/superbox/superbox-full-2.jpg" alt="My first photoshop layer mask on a high end PSD template theme" title="Bridge of Edgen" class="superbox-img">
        </div>

gallery.js

Template.gallery.rendered = function(){
    $('.superbox').SuperBox();
}

Template.gallery.helpers({
    gallery: function(){
        return Gallery.find();
    }
});

例如, Image 1Image 2

最好的问候, 太棒了

编辑:

我用过这种方法,效果很好,虽然在meteor docs中没有找到defer方法!

_.defer(function () {
  $('.superbox').SuperBox();
});

【问题讨论】:

  • 你确定你在创建模板时得到了Gallery中的所有文件吗?
  • 在你的渲染中不应该是 $('.superbox-list') 而不是 $('.superbox') 吗?
  • Peppe:是的,我在 Gallery 中拥有所有文档,这可能在图 1 中看到。Dave:我认为这不是问题,因为每个 jquery 都运行良好。

标签: javascript jquery meteor


【解决方案1】:

我用过这种方法,效果很好,虽然在meteor docs中没有找到defer方法!

_.defer(function () {
  $('.superbox').SuperBox();
});

【讨论】:

  • 这是underscore 方法,而不是Meteor 方法。记录下划线不是 Meteor 的工作!这仍然有一个问题,如果您加载页面然后将新的 Gallery 对象添加到数据库中,它将显示但不会被“超级装箱”。
【解决方案2】:

您的问题是可能第一次呈现您的画廊时数据不存在。您可以通过使用 iron-router 的 waitOn(如果您使用的是 iron-router)等待订阅来解决这个问题,但这只能解决页面加载时数据库中的文档问题。任何新文档也不会是“superBox'd”。所以解决这个问题的方法是为每个 Gallery 行设置一个特定的模板。 你的每个变成:

{{#each gallery}}
    {{> galley_row }}
{{/each}}

您添加一个新模板:

<template name="gallery_row">
    <div class="superbox-list">
        <img src="images/superbox/superbox-thumb-1.jpg" data-img="images/superbox/superbox-full-1.jpg" alt="My first photoshop layer mask on a high end PSD template theme" title="Miller Cine" class="superbox-img">
    </div>
</template>

然后每当你的新模板被渲染(#each 循环的每次迭代):

Template.galley_row.rendered = function () {
    $('.superbox').SuperBox();
});

【讨论】:

  • 我试图这样做,但我没有成功。 Jquery 没有反应。
  • 试试我修改后的渲染,我删除了this.$('.superbox-list')并用$('.superbox')替换它
猜你喜欢
  • 2016-07-31
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 2015-06-06
  • 2017-05-13
相关资源
最近更新 更多