javascript - 在Meteor中,定义辅助函数的旧方法与新方法不冲突吗?

标签 javascript meteor helper

我是 Meteor 新手,并尝试遵循“您的第一个 Meteor 应用程序”: http://meteortips.com/first-meteor-tutorial/ 我尝试定义一个辅助函数。 在我编写的html文件中:

<head>
    <title>Leaderboard</title>
</head>
<body>
    <h1>Leaderboard</h1>
    {{> leaderboard}}
</body>

<template name="leaderboard">
    <!-- Hello World -->
    <!-- {{player}} -->
    <!-- {{otherHelperFunction}} -->
<ul>
    {{#each player}}
        <li>{{name}}:{{score}}</li>
    {{/each}}
</ul> 

{{numOfPlayer}}
</template>

在我编写的JS文件中:

if(Meteor.isClient){
    Template.leaderboard.helpers({
        "player": function(){
            // return "Some other text";
            return PlayersList.find();
        },
        "numOfPlayer": function(){
            // return "Some other text";
            return PlayersList.find().count();
        },
        "otherHelperFunction": function(){
            return "Some other funciton";
        }
    })
    Template.leaderboard.player = function(){
    return "Some other text";
    }
    // console.log("Hello client");

}

if(Meteor.isServer){
    console.log("Hello server");
}

PlayersList = new Mongo.Collection('players');

因此,在客户端部分的 JS 文件中,我定义了两个“玩家”辅助函数:一个采用旧方式,一个采用新方式。旧的方式实际上是我忘记注释掉,但是当我运行这个项目时,该网站结果是以“新方式”执行的,并且似乎旧方式定义的“玩家”辅助函数并没有影响该项目所有和编译器并没有说存在任何错误或歧义(因为您可以看到这两个“播放器”辅助函数是为不同的功能定义的)。这是什么原因呢?是否是因为旧方式定义的辅助函数被新辅助函数覆盖了?

这是输出接口(interface)。 Here is the output interface

最佳答案

来自Meteor source code :

The first identifier in a path is resolved in one of two ways:

  1. Indexing the current data context. The identifier foo refers to the foo property of the current data context object.

  2. As a template helper. The identifier foo refers to a helper function (or constant value) that is accessible from the current template.

Template helpers take priority over properties of the data context.

您的 Template.leaderboard.player 函数定义位于数据上下文中,因此 Blaze 首先查找模板助手。由于您已经定义了帮助程序,因此它优先并被执行。

关于javascript - 在Meteor中,定义辅助函数的旧方法与新方法不冲突吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36060548/

相关文章:

angular - alanning :role, 在 angular2-meteor 中导入问题

ruby-on-rails - 如何从 rails 模块访问 URL 助手

ruby-on-rails - Rails 命名空间助手

javascript - 使用 setTimeout 时从函数返回值

javascript - Google Map API BackBoneJS 无法读取 null 的属性 'offsetWidth'

javascript - $parse、$interpolate 和 $compile 服务有什么区别?

javascript - 使用 JS 随机化 RGB 颜色

javascript - 在 Meteor 中如何将数据从内部函数复制到其他模板?

meteor - 在 Meteor 的模板中呈现任意嵌套对象的公认方法是什么?

ruby-on-rails - View 在浏览器中正确呈现,但在辅助方法上测试失败?