ifとtemplateの組み合わせ

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>ifとtemplateの組み合わせ</title>
  6. <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
  7. <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
  8. </head>
  9. <body>
  10. <h1>ifとtemplateの組み合わせ</h1>
  11. <h2>Participants</h2>
  12. Here are the participants:
  13. <div data-bind="foreach: people">
  14.   <div data-bind="if: name == 'Mario'">
  15.     <div data-bind="template: {name: 'person-template'}"></div>
  16.   </div>
  17.   <div data-bind="if: name == 'Franklin'">
  18.     <div data-bind="template: {name: 'person-template2'}"></div>
  19.   </div>
  20. </div>
  21. <script type="text/html" id="person-template">
  22.   <h3 data-bind="text: name"></h3>
  23.   <p>Credits: <span data-bind="text: credits"></span></p>
  24. </script>
  25. <script type="text/html" id="person-template2">
  26.   <h3 data-bind="text: name"></h3>
  27.   <b style="color:red;">Credits: <span data-bind="text: credits"></span></b>
  28. </script>
  29. <script type="text/javascript">
  30.   function MyViewModel() {
  31.     this.people = [
  32.       { name: 'Franklin', credits: 250 },
  33.       { name: 'Mario', credits: 5800 }
  34.     ]
  35.   }
  36.   ko.applyBindings(new MyViewModel());
  37. </script>
  38. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  39. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  40. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  41. </div>
  42. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  43. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  44. </div>
  45. </body>
  46. </html>