如何使用Mustaches.js获取CouchDB中的附件列表
JSON示例:
{
"_id": "t",
"_rev": "9-5eed240a008b0eb6efbaf9a439c43279",
"_attachments": {
"Doc1.pdf": {
"content_type": "application/pdf",
"revpos": 8,
"digest": "md5-pxnGZT6uqX4n2+vNNIOs/g==",
"length": 200633,
"stub": true
},
"Doc2.pdf": {
"content_type": "application/pdf",
"revpos": 6,
"digest": "md5-fxnGZT6uqX2n2+vNNI41s/g==",
"length": 100333,
"stub": true
}
}
}
我的模板如下所示:
{{^isAttEmpty}}
<p>Lista załączników:<p>
<ul>
{{#_attachments}}
<li>{{@key}} - URL:{{This will be URL to Image}}</li>
{{/_attachments}}
</ul>
{{/isAttEmpty}}
Mustache.js是否已构建函数来迭代对象?或者我应该在发送到Mustaches之前解析为数组吗?
答案 0 :(得分:3)
据我所知,Mustache.js只能遍历数组成员,而不是对象键。
在show
函数中,您必须格式化要发送给Mustache的对象。
这是一个可用于将附件转换为数组的函数:
function formatAttachments(attachments, docID) {
var result = [];
for (a in attachments) {
result.push({
name: a,
size: Math.round(attachments[a].length/104857.6)/10,
url: docID + "/" + a
});
}
return result;
}