如何避免流星助手多次运行

编程入门 行业动态 更新时间:2024-10-10 01:17:50
本文介绍了如何避免流星助手多次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的模板中,我有这个 HTML:

In my template I have this HTML:

<input id="name" type="text" value="{{card.name}}"> <input id="prefix" type="text" value="{{card.prefix}}"> <input id="phone" type="tel" value="{{card.phone}}">

和这个 JavaScript

and this JavaScript

Template.cardForm.helpers({ card: function() { return getCard(); } }); var getCard = function() { console.log("I'm here !!!"); return Cards.findOne({_id: cardId}); }

当我运行我的应用程序时,console.log 显示我在这里!!!"3 次,我认为 Ecards.findOne() 执行了 3 次.

When I run my app, the console.log shows "I'm here !!!" 3 times, and I think that Ecards.findOne() is executing 3 times.

我怎样才能避免那些额外的电话?

How can I Avoid those extra calls?

我想获取卡片对象以填充 {{card.name}}、{{card.prefix}} 和 {{card.phone}},但只能调用一次 getCard().

I want to get card object in order to fill {{card.name}}, {{card.prefix}} and {{card.phone}}, but only with one call to getCard().

推荐答案

card 助手被执行多次,因为 card 在评估的模板代码中出现不止一次.

The card helper is executed several times since card is present in evaluated template code more than once.

在您面临的场景中避免重复调用的一种模式是使用 #with:

A pattern to avoid duplicate calls in scenarios like the one you're facing is to use #with:

{{#with card}} <input id="name" type="text" value="{{name}}"> <input id="prefix" type="text" value="{{prefix}}"> <input id="phone" type="tel" value="{{phone}}"> {{/with}}

这将调用 card 一次,然后在其结果的上下文中运行嵌套代码.

This will call card once and then run the nested code in the context of its result.

更多推荐

如何避免流星助手多次运行

本文发布于:2023-10-19 17:44:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流星   助手

发布评论

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

>www.elefans.com

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