gpt4 book ai didi

templates - Meteor:在用数据渲染模板后调用函数

转载 作者:行者123 更新时间:2023-12-01 10:46:37 25 4
gpt4 key购买 nike

我有很多帖子想在轮播中显示。对于轮播,我使用 OwlCarousel .

    <div class="owl-carousel" id="featured-carousel">
{{#each featuredPosts}}
<div>
<h2>
{{postTitle}}
</h2>
</div>
{{/each}}
</div>

我这样称呼轮播:

Template.featuredCarousel.rendered = function(){
$('#featured-carousel').owlCarousel({
loop:true,
autoplay:true,
autoplayTimeout:3000,
items:1,
smartSpeed:1080,
padding:80
});
this.rendered = true;
};

我得到的结果是 Owl 基本上认为我只有一个项目要显示在轮播中,它是多个 div。显然,Template.featuredCarousel.rendered 中的函数在模板的#each-part 完成之前或数据到达之前被调用。

如何使实例化轮播的函数仅在模板完全呈现(包括所有数据)后调用?

非常感谢您的帮助。

P.S.:我使用 iron-router 进行路由,如下所示:

Router.map(function(){
this.route('home', {
path: '/',
waitOn: function(){
return Meteor.subscribe('featured');
},
data: function(){
return {featuredPosts: Featured.find({})};
}
});
});

P.P.S.:我也尝试过使用加载模板,但这也无济于事。

最佳答案

您已正确地指出您的问题:

What apparently happens is that the function inside Template.featuredCarousel.rendered is called before the #each-part of the template is completed or before the data has arrived.

模板的 rendered 回调仅在您的模板实例首次插入 DOM 时调用一次,因此如果您的数据尚未准备好(从服务器获取)但#each block 将不会不生成任何 HTML 元素,当您实例化轮播时,它将显示为空。

您可以做的是确保您的数据在 rendered 回调触发之前准备就绪。显然你已经尝试设置这个解决方案但没有成功,你确定你像这样添加了默认加载 Hook 吗?

Router.onBeforeAction("loading");

更好的解决方案是在呈现的回调中监听数据库更改,并在首次获取项目时相应地重新初始化轮播,然后动态添加和/或删除项目。

HTML

<template name="carousel">
<div class="owl-carousel">
{{#each featuredPosts}}
{{! item declaration}}
{{/each}}
</div>
</template>

JS

function featuredPosts(){
return Featured.find();
}

Template.carousel.helpers({
// feed the #each with our cursor
featuredPosts:featuredPosts
});

Template.carousel.rendered=function(){
var options={...};
// first initialization
this.$(".owl-carousel").owlCarousel(options);
this.autorun(_.bind(function(){
// this is how we "listen" for databases change : we setup a reactive computation
// and inside we reference a cursor (query) from the database and register
// dependencies on it by calling cursor.forEach, so whenever the documents found
// by the cursor are modified on the server, the computation is rerun with
// updated content, note that we use the SAME CURSOR that we fed our #each with
var posts=featuredPosts();
// forEach registers dependencies on the cursor content exactly like #each does
posts.forEach(function(post){...});
// finally we need to reinit the carousel so it take into account our newly added
// HTML elements as carousel items, but we can only do that AFTER the #each block
// has finished inserting in the DOM, this is why we have to use Deps.afterFlush
// the #each block itself setup another computation and Deps.afterFlush makes sure
// this computation is done before executing our callback
Tracker.afterFlush(_.bind(function(){
this.$(".owl-carousel").data("owlCarousel").reinit(options);
},this));
},this));
};

我不熟悉 owl-carousel,所以我不确定 reinit 是否会正常运行(我已经快速检查了文档,但似乎没问题)。

对于没有 reinit 方法的类似 JS 插件(例如 bootstrap carousel),您可以先检查插件是否已实例化,然后销毁它并重新创建它,如下所示:

var carousel=this.$(".carousel").data("bs.carousel");
if(carousel){
// bootstrap carousel has no destroy either so we simulate it by pausing it
// and resetting the data attribute to null (it's a bit messy)
this.$(".carousel").carousel("pause");
this.$(".carousel").data("bs.carousel",null);
}
// initialize normally because previous instance was killed
this.$(".carousel").carousel({...});

关于templates - Meteor:在用数据渲染模板后调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25354070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com