LoginSignup
4
4

More than 5 years have passed since last update.

Ractive.js で ハッシュをリスト展開して表示するベストプラクティスを教えて下さい

Last updated at Posted at 2014-02-16

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>

これでも ハッシュのキーと値をリスト表示できるし、ハッシュの値を変更すれば、リスト表示も変わるようになるけど、正当なやり方なんだろうか?

4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4