我是 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 文件中,我定义了两个“玩家”辅助函数:一个采用旧方式,一个采用新方式。旧的方式实际上是我忘记注释掉,但是当我运行这个项目时,该网站结果是以“新方式”执行的,并且似乎旧方式定义的“玩家”辅助函数并没有影响该项目所有和编译器并没有说存在任何错误或歧义(因为您可以看到这两个“播放器”辅助函数是为不同的功能定义的)。这是什么原因呢?是否是因为旧方式定义的辅助函数被新辅助函数覆盖了?
最佳答案
The first identifier in a path is resolved in one of two ways:
Indexing the current data context. The identifier foo refers to the foo property of the current data context object.
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/