当我尝试在选择器位于子模板中的模板上使用它时,findall返回任何内容。

这是html:

<template name="products">
    {{#each productList}}
        {{> product }}
    {{/each}}
</template>
<template name="product">
    <div class="box">{{name}}</div>
</template>
.

这是JS:

Template.products.helpers({
    productList: function() {
        var all = Products.find({}).fetch();
        return all;
    }
});
Template.products.rendered = function(){
    var boxes = this.findAll('.box');
    console.log(boxes.length);
}
.

框的输出.Length是0.任何想法我如何获得“框”元素?

有帮助吗?

解决方案 3

这是正确的答案。我已将其添加到我的铁路路由:

action : function () {
    if (this.ready()) {
        this.render();
    }
}
.

找到了 https://stachoverflow.com/a/23576039/130237 在我试图解决一个不同的问题。

其他提示

这是在装入所有产品后运行脚本所做的。

我在所有产品中添加了Last_Product属性。

Template.products.helpers({
    productList: function() {
        var all = Products.find({}).fetch();
        var total = all.length;
        var ctr = 0;
        all.forEach(function(doc){
            doc.last_product = false;

            ctr++;
            if(ctr == total)
            {
                doc.last_product = true;
            }
            return doc;
        });
        return all;
    }
});
. 然后

而不是“template.products”,我使用“template.product”来检测是否呈现了最后一个产品。当呈现最后一个产品时,运行脚本。

Template.product.rendered = function(){
    if(this.data.last_product){
        var boxes = $('.pbox');
        console.log(boxes.length);
    }
}
.

boxes.length现在具有正确的长度。

感谢David的想法!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top