/public/javascripts/application.js

https://github.com/admanb/tournaments · JavaScript · 290 lines · 215 code · 74 blank · 1 comment · 6 complexity · dd4e5f86d6ac27bfd15e0d02c1496cfc MD5 · raw file

  1. _.templateSettings = {
  2. interpolate: /\{\{(.+?)\}\}/g
  3. };
  4. var Tournament, Tournaments, Player, Players, TournamentRow, TournamentList, TournamentForm,
  5. PlayerRow, PlayerList, PlayerForm, AppView;
  6. Tournament = Backbone.Model.extend({
  7. Collection: Tournaments,
  8. url: /tournaments.json/
  9. });
  10. Tournaments = Backbone.Collection.extend({
  11. url: '/tournaments.json',
  12. model: Tournament
  13. });
  14. var Tournaments = new Tournaments();
  15. Player = Backbone.Model.extend({
  16. Collection: Players,
  17. url: '/players.json/'
  18. });
  19. Players = Backbone.Collection.extend({
  20. url: '/players.json',
  21. model: Player
  22. });
  23. var Players = new Players();
  24. Participant = Backbone.Model.extend({
  25. Collection: Participants,
  26. url: '/participants.json'
  27. });
  28. Participants = Backbone.Collection.extend({
  29. url: '/participants.json',
  30. model: Participant
  31. });
  32. var Participants = new Participants();
  33. TournamentView = Backbone.View.extend({
  34. el: $('#tournament-view'),
  35. initialize: function() {
  36. _.bindAll(this, 'render');
  37. this.selectedTournament = null;
  38. },
  39. render: function(tournament) {
  40. this.el.find('#title').html(tournament.get('title'));
  41. this.el.find('#description').html(tournament.get('description'));
  42. this.el.show();
  43. this.selectedTournament = tournament;
  44. }
  45. })
  46. TournamentRow = Backbone.View.extend({
  47. tagName: 'li',
  48. events: {
  49. 'click a': 'selectTournament'
  50. },
  51. template: _.template($('#tournament-row-template').html()),
  52. initialize: function() {
  53. _.bindAll(this, 'render', 'selectTournament');
  54. },
  55. selectTournament: function() {
  56. appView.tournamentList.remove();
  57. appView.tournamentForm.remove();
  58. appView.tournamentView.render(this.model);
  59. },
  60. render: function() {
  61. $(this.el).html(this.template({
  62. id: this.model.id,
  63. title: this.model.get('title')
  64. }))
  65. return this;
  66. }
  67. });
  68. TournamentList = Backbone.View.extend({
  69. el: $('#tournament-info'),
  70. events: {
  71. },
  72. initialize: function() {
  73. _.bindAll(this, 'render', 'addTournament', 'selectTournament');
  74. this.collection.bind('reset', this.render);
  75. },
  76. addTournament: function(t) {
  77. var index = Tournaments.indexOf(t) + 1;
  78. t.rowView = new TournamentRow({ model: t });
  79. var el = this.el.find('li:nth-child(' + index + ')');
  80. if (el.length) {
  81. el.after(t.rowView.render().el);
  82. } else {
  83. this.el.append(t.rowView.render().el);
  84. }
  85. },
  86. selectTournament: function(t) {
  87. //stub
  88. },
  89. create: function(title, description) {
  90. var t = new Tournament({ title: title, description: description })
  91. t.save();
  92. Tournaments.add(t);
  93. this.addTournament(t);
  94. },
  95. render: function(tournaments) {
  96. var tournamentList = this;
  97. tournaments.each(function(t) {
  98. tournamentList.addTournament(t);
  99. });
  100. }
  101. });
  102. TournamentForm = Backbone.View.extend({
  103. el: $('#tournament-form'),
  104. events: {
  105. 'click #tournament-save-button': 'save'
  106. },
  107. initialize: function(model) {
  108. _.bindAll(this, 'save', 'clearForm');
  109. },
  110. save: function(e) {
  111. e.preventDefault();
  112. var title = this.el.find('input.title').val(),
  113. description = this.el.find('textarea.description').val();
  114. this.clearForm();
  115. appView.tournamentList.create(title, description);
  116. },
  117. clearForm: function() {
  118. this.el.find('input.title').val('');
  119. this.el.find('textarea.description').val('');
  120. }
  121. });
  122. PlayerRow = Backbone.View.extend({
  123. tagName: 'li',
  124. events: {
  125. 'click a': 'selectPlayer'
  126. },
  127. template: _.template($('#player-row-template').html()),
  128. initialize: function() {
  129. _.bindAll(this, 'render', 'selectPlayer');
  130. },
  131. selectPlayer: function() {
  132. appView.playerList.selectPlayer(this);
  133. },
  134. render: function() {
  135. $(this.el).html(this.template({
  136. id: this.model.id,
  137. first: this.model.get('first'),
  138. last: this.model.get('last')
  139. }));
  140. return this;
  141. }
  142. });
  143. PlayerList = Backbone.View.extend({
  144. el: $('#player-list'),
  145. events: {
  146. },
  147. initialize: function() {
  148. _.bindAll(this, 'render', 'addPlayer', 'selectPlayer');
  149. this.collection.bind('reset', this.render);
  150. this.selectedPlayer = null;
  151. },
  152. addPlayer: function(p) {
  153. var index = Players.indexOf(p) + 1;
  154. p.rowView = new PlayerRow({ model: p });
  155. var el = this.el.find('li:nth-child(' + index + ')');
  156. if (el.length) {
  157. el.after(p.rowView.render().el);
  158. } else {
  159. this.el.append(p.rowView.render().el);
  160. }
  161. },
  162. selectPlayer: function(p) {
  163. var t = appView.tournamentView.selectedTournament;
  164. if(t != null) {
  165. var participant = new Participant({ player: p.id, tournament: t.id })
  166. participant.save();
  167. Participants.add(participant);
  168. appView.tournamentView.addParticipant(participant);
  169. }
  170. },
  171. create: function(first, last) {
  172. var p = new Player({ first: first, last: last })
  173. p.save();
  174. Players.add(p);
  175. this.addPlayer(p);
  176. },
  177. render: function(players) {
  178. var playerList = this;
  179. players.each(function(p) {
  180. playerList.addPlayer(p);
  181. });
  182. }
  183. });
  184. PlayerForm = Backbone.View.extend({
  185. el: $('#player-form'),
  186. events: {
  187. 'click #player-save-button': 'save'
  188. },
  189. initialize: function(model) {
  190. _.bindAll(this, 'save', 'clearForm');
  191. },
  192. save: function(e) {
  193. e.preventDefault();
  194. var first = this.el.find('input.first').val(),
  195. last = this.el.find('input.last').val();
  196. this.clearForm();
  197. appView.playerList.create(first, last);
  198. },
  199. clearForm: function() {
  200. this.el.find('input.first').val('');
  201. this.el.find('input.last').val('');
  202. }
  203. });
  204. AppView = Backbone.View.extend({
  205. initialize: function() {
  206. this.playerList = new PlayerList({ collection: Players });
  207. this.playerForm = new PlayerForm();
  208. this.tournamentList = new TournamentList({ collection: Tournaments });
  209. this.tournamentForm = new TournamentForm();
  210. this.tournamentView = new TournamentView();
  211. }
  212. });
  213. var appView = new AppView();
  214. window.Players = Players;
  215. window.Tournaments = Tournaments;
  216. window.appView = appView;