【发布时间】:2015-09-01 06:59:10
【问题描述】:
我的 Meteor 项目中有这个 html:
<head>
<title>The Dentist Hurt My Fillings</title>
</head>
<body>
<h2>Thirteen Ways of Looking at a Duckbilled Platypus</h2>
<br/>
<br/>
<div class="container">
{{> whizzardBlizzard}}
</div>
</body>
<template name="whizzardBlizzard">
<form>
{{#if firstStep}}
{{> firstStepTemplate}}
{{/if}}
{{#if secondStep}}
{{> secondStepTemplate}}
{{/if}}
{{#if thirdStep}}
{{> thirdStepTemplate}}
{{/if}}
<input type="submit" value="Submit" class="button">
</form>
</template>
<template name="firstStepTemplate">
<h2>Step 1</h2>
</template>
<template name="secondStepTemplate">
<h2>Step 2</h2>
</template>
<template name="thirdStepTemplate">
<h2>Step 3</h2>
</template>
...还有这个 Javascript:
if (Meteor.isClient) {
// stepNum starts at 1
Session.setDefault('stepNum', 1);
Template.whizzardBlizzard.events({
"submit form": function (event) {
//event.preventDefault();
// save the vals to a new document in a collection
Session.set('stepNum', Session.get('stepNum') + 1);
}
});
Template.whizzardBlizard.helpers({
'firstStep': function() {
return (Session.get('stepNum') == 1);
},
'secondStep': function() {
return (Session.get('stepNum') == 2)
},
'thirdStep': function() {
return (Session.get('stepNum') == 3)
}
// . . . etc.
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
当我尝试运行它时,我得到“未捕获的类型错误:无法读取未定义的属性 'helpers'”?
怎么可能?模板助手是 Meteor 的一个关键组件,它的使用示例镜像我的。
我试过用单引号括起来和不括起来助手名称(例如“firstStep”);也就是说,我已经尝试了这两个:
firstStep: function() {
..还有这个:
'firstStep': function() {
...虽然这样称呼它:
{{#if firstStep}}
{{> firstStepTemplate}}
{{/if}}
那么为什么据说“助手”不可读?
【问题讨论】:
-
我几乎可以肯定它归结为加载顺序问题:您尝试在定义模板本身之前定义您的助手。
-
那我该怎么做才能避免这种情况呢?
-
因为它是 ..未定义。那么那么; 为什么?调试器 - 如果停止 - 将非常擅长显示当前状态,包括检查堆栈。
-
不,它不是未定义的。问题是,假设 Kyll 是对的,如何安排事情,以便在执行 .html 文件中的定义之前不从 .js 文件中引用它。
标签: javascript meteor meteor-blaze meteor-helper