如何在#each完成后执行回调?

编程入门 行业动态 更新时间:2024-10-24 07:24:09
本文介绍了如何在#each完成后执行回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

#each 完成后,我遇到回调问题。我有一个名为content的模板:

I'm having trouble with a callback after the #each has finished. I have a template named "content":

<template name="content"> {{#if Template.subscriptionsReady}} {{#each currentData}} <div data-cid="{{this._id}}"></div> {{/each}} {{else}} Loading... {{/if}} </template>

首先我等待订阅,如果可用,我用 {{#each}} 并附加 div 。我需要的是for-each循环完成时的一种回调(换句话说就是DOM就绪)。

At first I wait for a subscription, when this is available, I iterate through my Collection with {{#each}} and append the div. What I need is a sort of callback for when the for-each loop is done (in other words DOM ready).

Template.content.onRendered()

- >触发到早期

我还尝试在{{each}}之后追加一个图像并在其 onload 中触发一个函数,如下所示:

I also tried appending an image after the {{each}} and fire a function in its onload like this:

<img style="height:0;width:0" src="*mysource*" onload="callback()">

- >有时工作但不可靠某种方式

-> did work sometimes but not reliable somehow

有没有办法获得此回调?我不担心改变这个模板的结构,如果它带来了解决方案。

Is there a way to get this callback? I do not fear to change the structure of this template, if that brings the solution.

推荐答案

我有类似的问题,之后很多搜索发现了以下解决方案。我尝试使用 Tracker , onRendered 和其他技巧,但都没有奏效。这可以被视为更多的黑客,但有效。不幸的是不记得我最初在哪里找到了这个解决方案

I had a similar problem and after a lot of searching found the following solution. I tried using Tracker, onRendered and other tricks, none of them worked. This could be considered more of a hack, but works. Unfortunately can't remember where I found this solution initially.

从模板开始,但在每个之后添加模板标签。

Start with your template, but add an template tag after your each.

<template name="content"> {{#if Template.subscriptionsReady}} {{#each currentData}} <div data-cid="{{this._id}}"></div> {{/each}} {{doneTrigger}} {{else}} Loading... {{/if}} </template>

然后定义一个返回null的助手。

Then define a helper that returns null.

Template.content.helpers({ doneTrigger: function() { Meteor.defer(function() { // do what you need to do }); return null; } });

您可以阅读更多关于 Meteor.defer() 此处,但相当于使用0毫秒 setTimeout 。

You can read more about Meteor.defer() here, but it is equivalent to using a 0 millisecond setTimeout.

更多推荐

如何在#each完成后执行回调?

本文发布于:2023-11-25 04:08:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   完成后   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!