我有一个 meteor 模板,它渲染一些我需要执行 jquery 函数的 html。现在,我已经设置了一个依赖项,以便每次与该模板关联的数据(对象列表)发生更改时,该函数都会运行。我完全不确定这是做我想做的事情的最佳方法,但每次我添加/删除/重新排列对象时它都会运行该函数,所以这是一个开始。
但是,该函数似乎在模板重新渲染之前运行,因此前一组 block 被 jquery 化,但我刚刚在该操作上添加的任何 block 都不会.
有没有办法强制函数在模板渲染/更新后运行?非常感谢任何帮助!
(这里有类似的问题 - 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 中?不确定。这似乎是放置它的好地方,但这是我第一次真正处理依赖关系,所以我在这里可能完全偏离基础。任何任何关于问题所在的想法都很好。)
最佳答案
您可以使用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!
}
关于jquery - 更新 meteor 模板后运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28615327/