Javascript JS-流星返回多种方法
我正试图找到一种方法来回报:Javascript JS-流星返回多种方法,javascript,meteor,Javascript,Meteor,我正试图找到一种方法来回报: list: function() { return binary.find(), o8.find(), dec.find(), h16.find(); }, 作为此模板的一部分 <template name="convert"> <div class="container-fluid"> <button id="buttonchange" class="btn btn-info {{butt
list: function() {
return binary.find(), o8.find(), dec.find(), h16.find();
},
作为此模板的一部分
<template name="convert">
<div class="container-fluid">
<button id="buttonchange" class="btn btn-info
{{buttonchangeclass}}">Convert Decimal</button>
{{#each list}}
<div class="row, rowbox">
<h5 class="base">Number: <span class="bd">{{decimal}}</span></h5>
<h5 class="base">Binary: <span class="bd">{{base2}}</span></h5>
<h5 class="base">Octal: <span class="bd">{{octal}}</span></h5>
<h5 class="base">Hex: <span class="bd">{{hex}}</span></h5>
<button id="buttonrem2" class="btn btn-warning {{buttonrem2class}}">Clear</button>
</div>
{{/each}}
</div>
</template>
十进制转换
{{{#每个列表}
编号:{{decimal}}
二进制:{{base2}}
八进制:{{Octal}
十六进制:{{Hex}}
清楚的
{{/每个}}
我对这两种系统都很陌生。所以,任何想法都会很好。正如克里皮可能说的,看起来你正在尝试建立一个角色表
让我们用两种不同的方法来说明Meteor、javascript和spacebars模板的不同方面
对象数组
Template.convert.helpers({
list: function(){
return [
{ base2: '0000', octal: '00' , decimal: 0 , hex: '00'},
{ base2: '0001', octal: '01' , decimal: 1 , hex: '01'},
{ base2: '1000', octal: '10' , decimal: 8 , hex: '08'},
{ base2: '1111', octal: '17' , decimal: 15 , hex: '0F'}
];
})
});
在这种情况下,每一行都有一个对象,其中包含希望在模板中显示的所有元素。在这里,list helper定义了所有数据,但在正常使用中,MongoDB中会保留一组数据,并使用collection.find()返回一个游标
用于格式化的辅助函数
因为您只是在进行格式转换,所以存储每个数字的每个格式似乎有些过分。让我们使用助手将数字从十进制转换为其他基数:
Template.convert.helpers({
list: function(){
return [0,1,8,15]; // same data as last time, but decimal only
},
base2: function(){
return this.toString(2);
},
octal: function(){
return this.toString(8);
},
hex: function(){
return this.toString(16);
}
});
现在,您可以轻松处理可能从列表帮助器返回的任何数字
这仍然有点冗长。如果你想用疯狂的基数来显示数字,比如基数7或12呢?您可以编写一个以基为参数的辅助函数:
Template.convert.helpers({
rebase: function(base){
return this.toString(base);
})
});
然后在模板中使用它与八进制等的{{rebase 8}}