在更新流星模板后运行函数 [英] Running a function AFTER a meteor template is updated

查看:120
本文介绍了在更新流星模板后运行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个meteor模板渲染一些html,我需要执行一个jquery函数。现在,我已经设置了一个依赖关系,所以每次绑定到该模板的数据(对象列表)改变时,函数就会运行。我不敢肯定这是做我想做的最好的方法,但它会运行该函数每次添加/删除/重新排列对象,这是一个开始。

I have a meteor template rendering some html that I need to perform a jquery function on. Right now, I've set up a dependency so that every time the data (a list of objects) tied to that template changes, the function runs. I'm not at all sure this is the best way to do what I'm trying to do, but it does run the function every time I add/delete/rearrange objects, so that's a start.

,该函数似乎在重新呈现模板之前运行,因此上一组块获得jquery-fied,

However, the function seems to be running before the template is re-rendered, so the previous set of blocks get jquery-fied, but any blocks I've just added on that action do not.

在渲染/更新模板之后,是否有一种强制函数运行的方法?非常感谢任何帮助!

Is there a way to force a function to run AFTER a template is rendered/updated? Any help is much appreciated!

(类似的问题在这里 - Meteor reactive jquery - 但它没有任何答案)

(similar question here -- Meteor reactive jquery -- but it doesn't have any answers)

这里是一些可能相关的代码位:

Here are some maybe-relevant code bits:

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的自动运行的东西?不知道这似乎是一个好地方,我们第一次真的处理依赖,所以我可以完全脱离这里。任何想法什么错误都会很好。)

(Why is the autorun in my Template.rendered thing? Not sure. That seemed like an okay place to put it, but this is my first time really dealing with dependencies, so I could be totally off-base here. Any ideas whatsoever on what's going wrong would be excellent.)

推荐答案

您可以使用 Tracker.afterFlush (以及 Tracker code>是 Deps 的新名称:

You can use Tracker.afterFlush for that (and Tracker is a new name for 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的方式更像这样:

However, the Meteor way is something more like this:

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

这篇关于在更新流星模板后运行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆