knockoutjsちゅーとりある:SPAでデータの取得

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>knockoutjsちゅーとりある:SPAでデータの取得</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. <style>
  9. body { font-family: Helvetica, Arial }
  10. input:not([type]), input[type=text], input[type=password], select { background-color: #FFFFCC; border: 1px solid gray; padding: 2px; }
  11. body { font-family: Helvetica, Arial}
  12. .folders { background-color: #bbb; list-style-type: none; padding: 0; margin: 0; border-radius: 7px;
  13.   background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #d6d6d6), color-stop(0.4, #c0c0c0), color-stop(1,#a4a4a4));
  14.   margin: 10px 0 16px 0;
  15.   font-size: 0px;
  16. }
  17. .folders li:hover { background-color: #ddd; }
  18. .folders li:first-child { border-left: none; border-radius: 7px 0 0 7px; }
  19. .folders li { font-size: 16px; font-weight: bold; display: inline-block; padding: 0.5em 1.5em; cursor: pointer; color: #444; text-shadow: #f7f7f7 0 1px 1px; border-left: 1px solid #ddd; border-right: 1px solid #888; }
  20. .folders li { *display: inline !important; } /* IE7 only */
  21. .folders .selected { background-color: #444 !important; color: white; text-shadow:none; border-right-color: #aaa; border-left: none; box-shadow:inset 1px 2px 6px #070707; }
  22. .mails { width: 100%; table-layout:fixed; border-spacing: 0; }
  23. .mails thead { background-color: #bbb; font-weight: bold; color: #444; text-shadow: #f7f7f7 0 1px 1px; }
  24. .mails tbody tr:hover { cursor: pointer; background-color: #68c !important; color: White; }
  25. .mails th, .mails td { text-align:left; padding: 0.4em 0.3em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  26. .mails th { border-left: 1px solid #ddd; border-right: 1px solid #888; padding: 0.4em 0 0.3em 0.7em; }
  27. .mails th:nth-child(1), .mails td:nth-child(1) { width: 20%; }
  28. .mails th:nth-child(2), .mails td:nth-child(2) { width: 15%; }
  29. .mails th:nth-child(3), .mails td:nth-child(3) { width: 45%; }
  30. .mails th:nth-child(4), .mails td:nth-child(4) { width: 15%; }
  31. .mails th:last-child { border-right: none }
  32. .mails tr:nth-child(even) { background-color: #EEE; }
  33. .viewMail .mailInfo { background-color: #dae0e8; padding: 1em 1em 0.5em 1.25em; border-radius: 1em; }
  34. .viewMail .mailInfo h1 { margin-top: 0.2em; font-size: 130%; }
  35. .viewMail .mailInfo label { color: #777; font-weight: bold; min-width: 2.75em; text-align:right; display: inline-block; }
  36. .viewMail .message { padding: 0 1.25em; }
  37. </style>
  38. <script type="text/javascript">
  39.   $(document).ready(function () {
  40.     function WebmailViewModel() {
  41.       var self = this;
  42.       // データ
  43.       self.folders = ['Inbox', 'Archive', 'Sent', 'Spam'];
  44.       
  45.       // データに対してIDをつけるけど、とりあえず何もなしと思われる。
  46.       self.chosenFolderId = ko.observable();
  47.       self.chosenFolderData = ko.observable();
  48.       // クリックしたときの処理。
  49.       self.goToFolder = function(folder) {
  50.         self.chosenFolderId(folder);
  51.         
  52.         // フォルダに合わせて読み込むファイルを変更
  53.         var fileName = "./" + folder + ".txt";
  54.         $.getJSON(fileName, {}, self.chosenFolderData);
  55.       };
  56.       
  57.       // 最初に表示するフォルダ
  58.       self.goToFolder('Inbox');
  59.     }
  60.     ko.applyBindings(new WebmailViewModel());
  61.   });
  62. </script>
  63. </head>
  64. <body>
  65. <!-- Folders -->
  66. <ul class="folders" data-bind="foreach: folders">
  67. <li data-bind="text: $data, css: { selected: $data == $root.chosenFolderId() }, click: $root.goToFolder"></li>
  68. </ul>
  69. <!-- Mails grid -->
  70. <table class="mails" data-bind="with: chosenFolderData">
  71.   <thead><tr><th>From</th><th>To</th><th>Subject</th><th>Date</th></tr></thead>
  72.   <tbody data-bind="foreach: mails">
  73.     <tr>
  74.       <td data-bind="text: from"></td>
  75.       <td data-bind="text: to"></td>
  76.       <td data-bind="text: subject"></td>
  77.       <td data-bind="text: date"></td>
  78.     </tr>
  79.   </tbody>
  80. </table>
  81. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  82. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  83. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  84. </div>
  85. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  86. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  87. </div>
  88. </body>
  89. </html>