【发布时间】:2015-07-03 12:26:31
【问题描述】:
我目前正在构建一个漂亮的小“天赋点分配器”视图,类似于流行的 RPG 游戏提供的视图。我不想要所有按钮和文本框的 HTML 代码的巨大墙,所以我创建了一个模板,我将两个参数传递给它:
- 我要更改的统计信息的名称
- stat 的初始值
模板正确呈现,我注意到当我将结果记录到控制台时,变量似乎已正确更改。但是,显示的值不会改变,会一直保持为 0。
这是模板本身:
<template name="attributeStepper">
<div class="row" style="margin: 1em;">
<div class="col-sm-3 col-md-2">
<h4>{{toUpper attribute}}</h4>
</div>
<div class="col-sm-6 col-md-4">
<div class="btn-group" role="group">
<button type="button" class="btn btn-default btn-value-dec">
<span class="glyphicon glyphicon-chevron-down"></span>
</button>
<button type="button" class="btn btn-default disabled">{{attributeValue}}</button>
<button type="button" class="btn btn-default btn-value-inc">
<span class="glyphicon glyphicon-chevron-up"></span>
</button>
</div>
</div>
</div>
</template>
这是我为模板定义的助手:
Template.attributeStepper.helpers({
toUpper : function(str) {
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
})
Template.attributeStepper.events({
'click .btn-value-inc' : function(event, tmpl) {
tmpl.data.attributeValue ++;
},
'click .btn-value-dec' : function(event, tmpl) {
tmpl.data.attributeValue --;
}
});
这就是我从实际视图中调用模板的方式:
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Attributes</h3>
</div>
{{ >attributeStepper attribute="strength" attributeValue="0"}}
{{ >attributeStepper attribute="courage" attributeValue="0"}}
{{ >attributeStepper attribute="intelligence" attributeValue="0"}}
{{ >attributeStepper attribute="agility" attributeValue="0"}}
{{ >attributeStepper attribute="dexterity" attributeValue="0"}}
{{ >attributeStepper attribute="intuition" attributeValue="0"}}
{{ >attributeStepper attribute="charisma" attributeValue="0"}}
</div>
我希望你能从中理解并告诉我我做错了什么,因为我觉得我还没有正确地遵循 Meteor 背后的思维方式。
干杯!
【问题讨论】:
标签: javascript meteor