流星助手总是有功能吗?

时间:2015-12-04 22:59:47

标签: javascript node.js meteor

流星助手必须是功能吗?我认为它们可以被直接分配给任何空格键可以理解的东西(数组,字符串,数字等)。但这并没有正常工作:

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())
    }]
}

通过"不能正常工作",我的意思是" tabButtons"在myTemplate中未定义。但是,这样可以正常工作:

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"
    }]

});

myTemplate2中未定义。我已经检查了具有相同名称的全局帮助程序,并且使用相同的名称将事物传递给每个上下文,只是为了确保其他内容不会返回undefined并覆盖我的函数,但这绝对不是导致问题的是什么。

这到底是怎么回事?

1 个答案:

答案 0 :(得分:0)

我明白了。问题出在Template.currentData(),而不是我使用的是函数。例如,这个:

'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.dataTemplate.currentData()只能在函数中定义直接传递给帮助器时才能工作。这是有道理的,现在我考虑一下,因为没有办法修改定义tabButtons的上下文,因为我包含Template.currentData()的行在它传递给它之前就已经被执行了Template.topicBuilder.helpers