在Meteor中设置helper中的复选框true或false值

时间:2014-12-30 16:21:27

标签: html meteor spacebars

如何将存储的true或false值应用于checked属性?

我有一系列的问题,用户可以打开/关闭(真/假)作为答案。我在Meteor.user()。profile中创建了一个名为questions的对象。字段名称是问题,存储的值可以是truefalse

enter image description here

现在我希望每个字段的属性显示存储的值,而不是重置为其默认值。

<template name="template">
 {{#with currentUser.profile.questions}}
 <div class="row">
 <input  data-name="profile.questions.school" type="checkbox"  name="questions" checked="{{school}}"> Go to School?
 </div>
 <div class="row">
 <input data-name="profile.questions.contributor" type="checkbox" name="questions" checked={{contributor}}"> contributor?
 </div>
 {{/with}}
</template>

我是这样做的漫长的方式吗?无论如何都会使用空格键{{isChecked}}方法辅助? https://www.discovermeteor.com/blog/spacebars-secrets-exploring-meteor-new-templating-engine/ 它似乎只是为了显示复选框。

1 个答案:

答案 0 :(得分:2)

我处理复选框/无线电的方式是这样的:

Template.myTemplate.helpers({
  isMyCheckboxChecked: function(value) {
    return (value == true ? 'checked' : '');
  }
});

然后在我的模板中我可以做到:

<input name="myCheckbox" type="checkbox" value="true" {{isMyCheckboxChecked myCheckboxValue}} />

这假设您传入一个值以确定是否需要选中该复选框。如果你的价值在各方面都非常真实/错误,我甚至会考虑使用Template.registerHelper使帮助者“全球化”。代码几乎是相同的,你只需要定义帮助稍微不同。这将使其在所有模板中都可用。

相关问题