gpt4 book ai didi

javascript - 在模板的上下文中从另一个助手调用一个助手(Meteor 0.9.4)

转载 作者:搜寻专家 更新时间:2023-11-01 04:25:01 25 4
gpt4 key购买 nike

从 Meteor 0.9.4 开始,定义 Template.MyTemplate.MyHelperFunction() 不再有效。

We deprecated the Template.someTemplate.myHelper = ... syntax in favor of Template.someTemplate.helpers(...). Using the older syntax still works, but it prints a deprecation warning to the console.

这对我来说似乎很好,因为它(至少)可以避免一些错误输入和重复的文本。然而,我很快发现我构建 Meteor 应用程序的方式依赖于这个新版本已弃用的功能。在我的应用程序中,我一直在使用旧语法定义助手/函数,然后从其他助手调用这些方法。我发现它帮助我保持代码的整洁和一致。

例如,我可能有这样一个控件:

//Common Method
Template.myTemplate.doCommonThing = function()
{
/* Commonly used method is defined here */
}

//Other Methods
Template.myTemplate.otherThing1 = function()
{
/* Do proprietary thing here */
Template.myTemplate.doCommonThing();
}

Template.myTemplate.otherThing2 = function()
{
/* Do proprietary thing here */
Template.myTemplate.doCommonThing();
}

但这似乎不适用于 Meteor 建议的新方法(这让我觉得我一直错了)。我的问题是,在模板的帮助程序之间共享通用的、特定于模板的逻辑的首选方法是什么?

最佳答案

抱歉,如果我很迟钝,但是您不能将函数声明为对象并将其分配给多个助手吗?例如:

// Common methods
doCommonThing = function(instance) // don't use *var* so that it becomes a global
{
/* Commonly used method is defined here */
}

Template.myTemplate.helpers({
otherThing1: function() {
var _instance = this; // assign original instance *this* to local variable for later use
/* Do proprietary thing here */
doCommonThing(_instance); // call the common function, while passing in the current template instance
},
otherThing2: function() {
var _instance = this;
/* Do some other proprietary thing here */
doCommonThing(_instance);
}
});

顺便说一下,如果您注意到您不断地在多个模板中复制相同的助手,那么使用 Template.registerHelper 可能会有所帮助,而不是将相同的函数分配给多个地方。

关于javascript - 在模板的上下文中从另一个助手调用一个助手(Meteor 0.9.4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26414985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com