Java客户端应用程序正在将数据插入到meteor的mongoDB中。将json对象发送到meteor的参数之一HashMap<String, MyObject>
转换为gson.toJson(HashMap<String, MyObject>)
。当前端接收Document / Collection对象时,它能够按名称获取此数据块,它看起来像这样:
"RANDOM_NAME1":{"service":"RANDOM_NAME1","count":20,"maxCount":300},
"RANDOM_NAME2":{"service":"RANDOM_NAME2,"count":50,"maxCount":340},
"
我不知道RANDOM_NAME可能是什么,任何字符串都是可能的,但大括号中的值总是3个参数 - service,count和maxCount。
这是我尝试循环遍历此数据块以分别显示3个参数:
HTML - 没有显示
<template name="displayData">
<h2> Data </h2>
{{#each top}}
{{#with interested_block}}
<p>"service: " {{service}} "count:" {{count}} "max: " {{maxCount}}</p>
{{/with}}
{{/each}}
</template>
HTML - 我知道RANDOM_NAME值的测试文档,仍然没有显示任何内容
<template name="displayData">
<h2> Data </h2>
{{#each top}}
{{#with interested_block}}
<p>{{RANDOM_NAME}} </p>
{{/with}}
{{/each}}
</template>
HTML - 我可以显示整个数据块(gson.toJson(HashMap<String, MyObject>
),如上所述)
<template name="displayData">
<h2> Data </h2>
{{#each top}}
<p>"java hashmap: " {{interested_block}}</p>
{{/each}}
</template>
此转换gson.toJson(HashMap<String, MyObject>)
是否无法解析,或者除了我尝试的内容之外还有其他展示方式?
答案 0 :(得分:0)
原因:
{{#each}}目前只接受数组,游标或falsey值。
解决方案:
将对象转换为数组(源Meteor and handlebars #each to iterate over object):
Handlebars.registerHelper('arrayify',function(obj){
result = [];
for (var key in obj) result.push(obj[key]);
return result;
});
假设你收到:
top = {
"RANDOM_NAME1":{"service":"RANDOM_NAME1","count":20,"maxCount":300},
"RANDOM_NAME2":{"service":"RANDOM_NAME2","count":50,"maxCount":340}
}
然后使用:
<template name="displayData">
<h2> Data </h2>
{{#each arrayify top}}
<p>"service: " {{service}} "count:" {{count}} "max: " {{maxCount}}</p>
{{/each}}
</template>