/Sample Apps/BorrowedGames/BorrowedGames/Scripts/app/requested.js

https://github.com/lefthandedgoat/Oak · JavaScript · 170 lines · 163 code · 7 blank · 0 comment · 7 complexity · 40afb7eef2a664e5311c54d3847f0d8b MD5 · raw file

  1. (function() {
  2. var requestedGame, requestedGameView, requestedGames, requestedGamesUrl, requestedGamesView;
  3. requestedGamesUrl = "";
  4. this.requested = {
  5. init: function(urls) {
  6. requestedGamesUrl = urls.requestedGamesUrl;
  7. return this.view = new requestedGamesView();
  8. },
  9. getRequestedGames: function() {
  10. return this.view.refresh();
  11. }
  12. };
  13. requestedGame = Backbone.Model.extend({
  14. name: function() {
  15. return this.get("Name");
  16. },
  17. console: function() {
  18. return this.get("Console");
  19. },
  20. requestedBy: function() {
  21. return this.get("RequestedBy").Handle;
  22. },
  23. daysOut: function() {
  24. return this.get("DaysOut");
  25. },
  26. shortName: function() {
  27. var name;
  28. name = this.name();
  29. if (name.length > 41) {
  30. name = name.substring(0, 40) + "... ";
  31. }
  32. return name += " (" + this.console() + ")";
  33. },
  34. giveGame: function(callback) {
  35. var _this = this;
  36. return $.post(this.get("GiveGame"), {}, function() {
  37. requested.getRequestedGames();
  38. return callback();
  39. });
  40. },
  41. gameReturned: function(callback) {
  42. var _this = this;
  43. return $.post(this.get("GameReturned"), {}, function() {
  44. requested.getRequestedGames();
  45. return callback();
  46. });
  47. },
  48. canGiveGame: function() {
  49. return !!this.get("GiveGame");
  50. }
  51. });
  52. requestedGames = Backbone.Collection.extend({
  53. model: requestedGame,
  54. url: function() {
  55. return requestedGamesUrl;
  56. }
  57. });
  58. requestedGamesView = Backbone.View.extend({
  59. el: "#requestedGames",
  60. initialize: function() {
  61. this.requestedGames = new requestedGames();
  62. this.requestedGames.bind('reset', this.render, this);
  63. return this.requestedGames.fetch();
  64. },
  65. refresh: function() {
  66. return this.requestedGames.fetch();
  67. },
  68. render: function() {
  69. var _this = this;
  70. $(this.el).empty();
  71. if (this.requestedGames.length === 0) {
  72. $("#requestedGamesHeader").hide();
  73. } else {
  74. $("#requestedGamesHeader").show();
  75. }
  76. return this.requestedGames.each(function(game) {
  77. return _this.addGame(game);
  78. });
  79. },
  80. addGame: function(game) {
  81. var view;
  82. view = new requestedGameView({
  83. model: game
  84. });
  85. view.render();
  86. return $(this.el).append(view.el);
  87. }
  88. });
  89. requestedGameView = Backbone.View.extend({
  90. tagName: "tr",
  91. events: {
  92. "click .check": "giveGame",
  93. "click .cancel": "gameReturned"
  94. },
  95. giveGame: function() {
  96. var el;
  97. el = this.el;
  98. return this.model.giveGame(function() {
  99. return $(el).find(".check").tooltip("hide");
  100. });
  101. },
  102. gameReturned: function() {
  103. var el;
  104. el = this.el;
  105. return this.model.gameReturned(function() {
  106. return $(el).find(".cancel").tooltip("hide");
  107. });
  108. },
  109. render: function() {
  110. var game;
  111. if (this.model.canGiveGame()) {
  112. game = this.genCanGiveTemplate();
  113. }
  114. if (!this.model.canGiveGame()) {
  115. game = this.genReturnGame();
  116. }
  117. $(this.el).html(game);
  118. return this;
  119. },
  120. genCanGiveTemplate: function() {
  121. var gen, requestedBy;
  122. gen = $.tmpl(this.canGiveGameTemplate, {
  123. requestedBy: this.model.requestedBy(),
  124. gameName: this.model.shortName()
  125. });
  126. requestedBy = this.model.requestedBy();
  127. gen.find(".check").tooltip({
  128. title: "<span style='font-size: 16px'>click this when you have given " + requestedBy + " the game, it'll start the count down for when the game needs to be returned</span>"
  129. });
  130. return gen;
  131. },
  132. genReturnGame: function() {
  133. var daysOut, gen;
  134. daysOut = this.model.daysOut();
  135. if (daysOut === 0) {
  136. daysOut = "just borrowed";
  137. } else {
  138. daysOut = daysOut.toString() + " day(s) so far";
  139. }
  140. gen = $.tmpl(this.returnGameTemplate, {
  141. requestedBy: this.model.requestedBy(),
  142. gameName: this.model.shortName(),
  143. daysOut: daysOut
  144. });
  145. gen.find(".cancel").tooltip({
  146. title: "<span style='font-size: 16px'>the game has been returned to me</span>"
  147. });
  148. return gen;
  149. },
  150. returnGameTemplate: '\
  151. <td>${requestedBy} is currently <span class="label label-success">borrowing</span> ${gameName} - ${daysOut}</td>\
  152. <td class="span2">\
  153. <i class="cancel icon-ok" style="cursor: pointer" href="javascript:;"></i>\
  154. </td>\
  155. ',
  156. canGiveGameTemplate: '\
  157. <td>${requestedBy} is <span class="label label-inverse">requesting</span> ${gameName}</td>\
  158. <td class="span2">\
  159. <i class="check icon-share-alt" style="cursor: pointer" href="javascript:;"></i>\
  160. </td>\
  161. '
  162. });
  163. }).call(this);