Ractive.jsのハッシュをリストに展開して表示するのはどうするのがスマートなんだろうか?
【追記】tekkoc さんに正当な方法を教えてもらいました。 tekkoc さんのコメントを参照ください(2014.02.14)
<!doctype html>
<head>
<title>Ractive.js - hash to list</title>
<script src="../bower_components/ractive/Ractive.js"></script>
</head>
<body>
<main id="main">
<script id="template" type="text/ractive">
<h2>ハッシュのリスト表示</h2>
<ul>
{{# toList(hash) }}
<li>
<span style="font-style: italic;">{{.}}</span>
:
<span style="font-weight: bold;">{{hash[.]}}</span>
</li>
{{/ end of hash}}
{{^ toList(hash) }}
<h4>no list :(</h4>
{{/ end of hash}}
</ul>
</script>
</main>
<script>
var ractive = new Ractive({
el: 'main'
, template: '#template'
, data: {
hash: {
a: 'AA'
, b: 'BB'
, c: 'CC'
}
, toList: function (hash) {
return Object.keys(hash)
}
}
})
</script>
</body>
これでも ハッシュのキーと値をリスト表示できるし、ハッシュの値を変更すれば、リスト表示も変わるようになるけど、正当なやり方なんだろうか?