运行 流星模板更新后的函数

Running a function AFTER a meteor template is updated

我有一个流星模板渲染了一些 html,我需要对其执行 jquery 功能。现在,我已经建立了一个依赖项,这样每当绑定到该模板的数据(对象列表)发生变化时,函数 运行s.我完全不确定这是做我想做的事情的最佳方式,但它会在每次我 add/delete/rearrange 对象时执行 运行 函数,所以这是一个开始。

然而,该函数似乎在模板重新渲染之前运行ning,所以之前的一组块得到jquery-fied ,但我刚刚在该操作上添加的任何块都没有。

有没有办法在模板 rendered/updated 之后强制函数 运行?非常感谢任何帮助!

(此处有类似问题 -- 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()

(为什么 auto运行 在我的 Template.rendered 中?不确定。这似乎是一个合适的地方,但这是我第一次真正处理依赖关系,所以我在这里可能完全偏离基地。任何关于出了什么问题的想法都是极好的。)

我找到了一些有用的东西,但它看起来还是有点老套...

我最后做的是设置超时。所以现在,我有:

而不是上面的代码
Template.sitePreview.rendered = () ->
  Deps.autorun () ->
    album = Albums.findOne(Session.get("selectedAlbum"))

    if (album)
      pictures = album.orderedPictures()
      setTimeout MyApp.loadJQuery, 500

到目前为止,模板 render/update 的时间已经足够了,因此 JQuery 在所有最新数据上运行。仍然希望有一个更优雅的解决方案,但这就可以了!

您可以使用 Tracker.afterFlushTrackerDeps 的新名称):

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()

然而,流星方式更像是这样:

<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!
}