在 Meteor 中,定义辅助函数的旧方法与新方法不冲突吗?
In Meteor, the old way to define a helper function is not conflict to the new way?
我是 Meteor 的新手,并尝试遵循 "Your First Meteor Application":
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 文件中,我定义了两个 "player" 辅助函数:一个以旧方式,一个以新方式。老办法其实是我忘记注释掉了,但是当我运行这个项目的时候,网站竟然是在"new way"执行的,好像是老办法定义的"player" helper函数根本没有影响项目,编译器也没有说有任何错误或歧义(因为你可以看到这两个 "player" 辅助函数是为不同的功能定义的)。这是什么原因?是不是旧方法定义的辅助函数被新辅助函数覆盖了?
这是输出接口。
因为新方法覆盖了旧方法。新方法分配新函数引用变量 'player'.
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 首先查找模板助手。由于您定义了帮助程序,因此它优先并被执行。
我是 Meteor 的新手,并尝试遵循 "Your First Meteor Application": 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 文件中,我定义了两个 "player" 辅助函数:一个以旧方式,一个以新方式。老办法其实是我忘记注释掉了,但是当我运行这个项目的时候,网站竟然是在"new way"执行的,好像是老办法定义的"player" helper函数根本没有影响项目,编译器也没有说有任何错误或歧义(因为你可以看到这两个 "player" 辅助函数是为不同的功能定义的)。这是什么原因?是不是旧方法定义的辅助函数被新辅助函数覆盖了?
这是输出接口。
因为新方法覆盖了旧方法。新方法分配新函数引用变量 'player'.
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 首先查找模板助手。由于您定义了帮助程序,因此它优先并被执行。