【问题标题】:Get attachments list in CouchDB using Mustaches.js使用 Mustaches.js 在 CouchDB 中获取附件列表
【发布时间】:2014-12-01 09:23:40
【问题描述】:

如何使用 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 之前解析到数组?

【问题讨论】:

    标签: javascript html templates couchdb mustache


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      相关资源
      最近更新 更多