使用 Mustaches.js 在 CouchDB 中获取附件列表

Get attachments list in CouchDB using Mustaches.js

本文关键字:获取 列表 CouchDB Mustaches js 使用      更新时间:2023-09-26

如何使用Mustaches在CouchDB中获取附件列表.js

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}}

胡子.js有内置的功能来迭代对象吗?还是我应该在发送到小胡子之前解析到数组?

据我所知,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;
}