我有一个流星模板渲染一些我需要执行jquery函数的html。现在,我已经设置了一个依赖项,这样每次绑定到该模板的数据(对象列表)都会发生变化时,该函数就会运行。我完全不确定这是我尝试做的最佳方式,但每次添加/删除/重新排列对象时它都会运行该功能,因此这是一个开始。
然而,在重新渲染模板之前,函数似乎正在运行,因此前一组块得到了jquery-fied,但是我刚刚在该操作上添加了任何块不要。
有没有办法强制在渲染/更新模板后运行函数?非常感谢任何帮助!
(类似的问题 - Meteor reactive jquery - 但它没有任何答案)
以下是一些可能相关的代码位:
Template.showPictures.rendered =
Deps.autorun () ->
album = Albums.findOne(Session.get("selectedAlbum"))
if (album)
pictures = album.orderedPictures()
MyApp.loadJQuery()
Template.showPictures.helpers
pics: ->
MyApp.myDependency.depend()
album = Albums.findOne(Session.get("selectedAlbum"))
album.orderedPictures()
(为什么我的Template.rendered中的自动运行?不确定。这似乎是一个好的地方,但这是我第一次真正处理依赖关系,所以我可以完全离开这里。任何任何关于出了什么问题的想法都会很棒。)
答案 0 :(得分:6)
您可以使用Tracker.afterFlush(Tracker
是Deps
的新名称):
Template.showPictures.rendered =
Tracker.autorun () ->
album = Albums.findOne(Session.get("selectedAlbum"))
if (album)
Tracker.afterFlush ->
// This will get executed when (among other things)
// the UI has been updated.
pictures = album.orderedPictures()
MyApp.loadJQuery()
然而,Meteor方式更像是这样:
<template name="things">
{{#each things}}
{{> thing}}
{{/each}}
</template>
<template name="thing">
<!-- Show the thing here... -->
</template>
Template.things.helpers({
things: function(){
return Things.find()
}
})
Template.thing.rendered = function(){
// This get executed each time a new thing is rendered!
}
答案 1 :(得分:0)
我发现了一些有效的东西,但它看起来仍然很糟糕......
我最终做的是设置超时。所以现在,我没有上面的代码,而是:
Template.sitePreview.rendered = () ->
Deps.autorun () ->
album = Albums.findOne(Session.get("selectedAlbum"))
if (album)
pictures = album.orderedPictures()
setTimeout MyApp.loadJQuery, 500
到目前为止,模板有足够的时间进行渲染/更新,因此JQuery可以运行所有最新的数据。仍然希望有一个更优雅的解决方案,但这样做!