获取Mustache.js中的函数返回的数组长度

时间:2019-02-19 17:24:47

标签: mustache

我已经有logged it as issue,但是我有以下用例,Mustache.js似乎不支持该用例:

我有一个IIFE,它具有返回数组的方法,并希望在Mustache模板中获取该返回数组的长度。

我尝试了以下所有操作:

HTML

<template id=sample-tmpl>
  {{#getSelectedData}}{{length}}{{/getSelectedData}}
</template>

<template id=sample-tmpl-no-pound-sign>
  {{getSelectedData}}{{length}}
</template>

<template id=sample-tmpl-dot-notation>
  {{getSelectedData().length}}
</template>

<template id=sample-tmpl-dot-notation-no-func-invoke>
  {{getSelectedData.length}}
</template>

<div id=target></div>
<div id=target-no-pound-sign></div>
<div id=target-dot-notation></div>
<div id=target-dot-notation-no-func-invoke></div>

JS

let data = (function() { 
  let _somePrivateDummyData = [
    {
      id : 1,
      name : 'bobbert',
      toString : () => `My name is ${this.name}`
    },
    {
      id : 2,
      name : 'tommy',
      toString : () => `My name is ${this.name}`
    }
  ]

  _somePrivateDummyData.forEach((obj) => obj.toString.bind(obj))

  /* a whole bunch of business logic irrelevant to this MVCE */

  return {
    getSelectedData : () => _somePrivateDummyData.slice()
    /* a smorgasboard of other exposed methods, irrelevant to this MVCE */
  }

})()


const versions = [
  '',
  '-no-pound-sign',
  '-dot-notation',
  '-dot-notation-no-func-invoke'
]

for (let version of versions) { 
  document.querySelector(`#target${version}`)
      .innerHTML = Mustache.render(document.querySelector(`#sample-tmpl${version}`).innerHTML,
                                     data)
}

这些都不起作用!

其中任何一个的预期输出:2 实际:[result of toString()], 0, whitespace

0 个答案:

没有答案