Javascript 流星助手应该永远是功能吗?
流星助手必须是功能吗?我认为它们可以直接分配给空格键可以理解的任何东西(数组、字符串、数字等)。但这并不正确:Javascript 流星助手应该永远是功能吗?,javascript,node.js,meteor,Javascript,Node.js,Meteor,流星助手必须是功能吗?我认为它们可以直接分配给空格键可以理解的任何东西(数组、字符串、数字等)。但这并不正确: Template.myTemplate.helpers = ({ 'tabButtons': [{ title: "Active Topics", link: "/dashboard/topic-builder/active", bootstrap: "col-xs-6", active: isActive("ac
Template.myTemplate.helpers = ({
'tabButtons': [{
title: "Active Topics",
link: "/dashboard/topic-builder/active",
bootstrap: "col-xs-6",
active: isActive("activeTopics", Template.currentData())
}, {
title: "Draft Topics",
link: "/dashboard/topic-builder/drafts",
bootstrap: "col-xs-6",
active: isActive("draftTopics", Template.currentData())
}]
}
“工作不正常”是指在myTemplate
中未定义“tabbutions”。但是,这样做很好:
Template.myTemplate.helpers = ({
'tabButtons': function(){
return [{
title: "Active Topics",
link: "/dashboard/topic-builder/active",
bootstrap: "col-xs-6",
active: isActive("activeTopics", Template.currentData())
}, {
title: "Draft Topics",
link: "/dashboard/topic-builder/drafts",
bootstrap: "col-xs-6",
active: isActive("draftTopics", Template.currentData())
}];
}
奇怪的是,我的代码中的其他地方也使用了同样的语法。例如,这:
Template.myTemplate2.helpers({
'sectionButtons': [{
title: "Matrix",
icon: "fa-table",
link: "/dashboard/overview",
bootstrap: "col-xs-2"
}, {
title: "Topic Builder",
icon: "fa-database",
link: "/dashboard/topic-builder",
bootstrap: "col-xs-2"
}, {
title: "Alerts",
icon: "fa-exclamation-circle",
link: "/dashboard/alerts",
bootstrap: "col-xs-2"
}, {
title: "Story",
icon: "fa-book",
link: "/dashboard/story-of-the-day",
bootstrap: "col-xs-2"
}, {
title: "Add Admin",
icon: "fa-plus",
link: "/dashboard/addadmin",
bootstrap: "col-xs-2"
}, {
title: "Settings",
icon: "fa-cog",
link: "/dashboard/settings",
bootstrap: "col-xs-2"
}]
});
'tabButtons': [{
title: "Active Topics",
link: "/dashboard/topic-builder/active",
bootstrap: "col-xs-6",
active: isActive("activeTopics", "activeTopics")
}, {
title: "Draft Topics",
link: "/dashboard/topic-builder/drafts",
bootstrap: "col-xs-6",
active: isActive("draftTopics", "draftTopics")
}]
在myTemplate2
中未定义。我已经检查了具有相同名称的全局帮助程序,以及传递给每个具有相同名称的上下文的内容,只是为了确保其他内容没有返回未定义和重写我的函数,但这肯定不是导致问题的原因
这到底是怎么回事?我知道了。问题出在
Template.currentData()
,而不是我在使用函数。例如,这:
Template.myTemplate2.helpers({
'sectionButtons': [{
title: "Matrix",
icon: "fa-table",
link: "/dashboard/overview",
bootstrap: "col-xs-2"
}, {
title: "Topic Builder",
icon: "fa-database",
link: "/dashboard/topic-builder",
bootstrap: "col-xs-2"
}, {
title: "Alerts",
icon: "fa-exclamation-circle",
link: "/dashboard/alerts",
bootstrap: "col-xs-2"
}, {
title: "Story",
icon: "fa-book",
link: "/dashboard/story-of-the-day",
bootstrap: "col-xs-2"
}, {
title: "Add Admin",
icon: "fa-plus",
link: "/dashboard/addadmin",
bootstrap: "col-xs-2"
}, {
title: "Settings",
icon: "fa-cog",
link: "/dashboard/settings",
bootstrap: "col-xs-2"
}]
});
'tabButtons': [{
title: "Active Topics",
link: "/dashboard/topic-builder/active",
bootstrap: "col-xs-6",
active: isActive("activeTopics", "activeTopics")
}, {
title: "Draft Topics",
link: "/dashboard/topic-builder/drafts",
bootstrap: "col-xs-6",
active: isActive("draftTopics", "draftTopics")
}]
很好
当我直接分配数组时,我认为Template.currentData()
可以工作,因为它是在助手中定义的。但是,使用template.data
和template.currentData()
似乎只有在直接传递给助手的函数中定义时才起作用。现在想想,这是有道理的,因为没有办法修改定义了tabButtons
的上下文,因为我包含Template.currentData()
的行在传递给Template.topicBuilder.helpers
之前就已经执行了