PageRenderTime 27ms CodeModel.GetById 45ms RepoModel.GetById 0ms app.codeStats 0ms

/js/backbonejs/router/backbonejs-router-sample.html

https://github.com/id774/sandbox
HTML | 149 lines | 125 code | 22 blank | 2 comment | 0 complexity | 8c339af75e0fb2d2ed6f23511a432948 MD5 | raw file
Possible License(s): Apache-2.0
  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Backbone Router Sample</title>
  6. </head>
  7. <body>
  8. <div id="main">
  9. <h1><a href="">Backbone Router Sample</a></h1>
  10. <div id="entries">
  11. </div>
  12. </div>
  13. <!--エントリをリスト表示するとき使うテンプレート-->
  14. <script id="entry-template" type="text/template">
  15. <div class="entry">
  16. <h3><a href="javascript:void(0)"><%- title %></a></h3>
  17. </div>
  18. </script>
  19. <!--エントリの詳細を表示するとき使うテンプレート-->
  20. <script id="detail-template" type="text/template">
  21. <h2><%- title %></h2>
  22. <div id="content">
  23. <%- content %>
  24. </div>
  25. </script>
  26. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
  27. <script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
  28. <script type="text/javascript" src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
  29. <script type="text/javascript">
  30. // エントリ。
  31. var Entry = Backbone.Model.extend({
  32. defaults: {
  33. title: "",
  34. content: ""
  35. }
  36. });
  37. // ブログ。
  38. var Blog = Backbone.Collection.extend({
  39. model: Entry
  40. });
  41. // エントリを表示するビュー。
  42. var EntryView = Backbone.View.extend({
  43. template: _.template($("#entry-template").html()),
  44. events: {
  45. "click a": "showDetail",
  46. },
  47. render: function() {
  48. $(this.el).html(this.template(this.model.toJSON()));
  49. return this;
  50. },
  51. // エントリタイトルをクリックしたら Router 経由で
  52. // 詳細ページに移動する
  53. showDetail: function() {
  54. window.router.navigate("entry/" + this.model.id, true);
  55. return false;
  56. }
  57. });
  58. // エントリの詳細を表示するビュー。
  59. var DetailView = Backbone.View.extend({
  60. template: _.template($("#detail-template").html()),
  61. render: function() {
  62. $(this.el).html(this.template(this.model.toJSON()));
  63. return this;
  64. }
  65. });
  66. // メインのビュー。
  67. // エントリの一覧を表示する。
  68. var BlogView = Backbone.View.extend({
  69. el: $("#main"),
  70. events: {
  71. "click h1>a": "showIndex"
  72. },
  73. render: function() {
  74. var entriesEl = $(this.el).find("#entries");
  75. $(entriesEl).empty();
  76. this.model.each(function(entry) {
  77. var view = new EntryView({ model: entry });
  78. view.render();
  79. $(entriesEl).append(view.el);
  80. }, this);
  81. return this;
  82. },
  83. // ブログタイトル(?) がクリックされたら Router 経由で
  84. // トップページを表示する。
  85. showIndex: function() {
  86. window.router.navigate("", true);
  87. return false;
  88. },
  89. // 渡された ID に該当するエントリの詳細を表示。
  90. showDetail: function(id) {
  91. var entry = this.model.get(parseInt(id));
  92. var view = new DetailView({ model: entry });
  93. view.render();
  94. var entriesEl = $(this.el).find("#entries");
  95. $(entriesEl).empty();
  96. $(entriesEl).append(view.el);
  97. }
  98. });
  99. // ルーター。
  100. var AppRouter = Backbone.Router.extend({
  101. // routes でルーターのメソッドと URL パターンをマッピングする。
  102. // ルーターのメソッドがリクエストハンドラになる。
  103. routes: {
  104. "entry/:id": "show",
  105. "entry/": "list",
  106. "": "list"
  107. },
  108. list: function() {
  109. window.App.render();
  110. },
  111. show: function(id) {
  112. window.App.showDetail(id);
  113. }
  114. });
  115. window.router = new AppRouter();
  116. var blog = new Blog();
  117. blog.add(new Entry({ id: 1, title: "foo", content: "hoge" }));
  118. blog.add(new Entry({ id: 2, title: "bar", content: "fuga" }));
  119. window.App = new BlogView({ model: blog });
  120. window.App.render();
  121. // 履歴監視スタート。
  122. // Backbone.history.start({ pushState: true }); で pushState は有効にできるけど、
  123. // Web サーバーでホストしないとうまく動かないので無効にしておく。
  124. Backbone.history.start();
  125. </script>
  126. </body>
  127. </html>