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

https://github.com/lefthandedgoat/Oak · JavaScript · 194 lines · 187 code · 7 blank · 0 comment · 7 complexity · 70b17c36a48f1d934db4355600c986a1 MD5 · raw file

  1. (function() {
  2. var libraries, library, preferredGameView, preferredGamesUrl, preferredGamesView;
  3. preferredGamesUrl = "";
  4. this.preferred = {
  5. init: function(urls) {
  6. preferredGamesUrl = urls.preferredGamesUrl;
  7. return this.view = new preferredGamesView();
  8. },
  9. getPreferredGames: function() {
  10. return this.view.refresh();
  11. }
  12. };
  13. library = Backbone.Model.extend({
  14. reviewUrl: function() {
  15. return "http://www.google.com/search?q=" + encodeURIComponent(this.name() + " ") + "site:gamespot.com&btnI=3564";
  16. },
  17. name: function() {
  18. return this.get("Name");
  19. },
  20. console: function() {
  21. return this.get("Console");
  22. },
  23. shortName: function() {
  24. var name;
  25. name = this.name();
  26. if (name.length > 41) {
  27. name = name.substring(0, 40) + "... ";
  28. }
  29. return name += " (" + this.console() + ")";
  30. },
  31. notInterested: function(callback) {
  32. var _this = this;
  33. return $.post(this.get("NotInterested"), {}, function() {
  34. _this.deleted = true;
  35. unwanted.getUnwantedGames();
  36. _this.change();
  37. return callback();
  38. });
  39. },
  40. isFavorited: function() {
  41. return this.get("IsFavorited");
  42. },
  43. favorite: function() {
  44. var _this = this;
  45. return $.post(this.get("FavoriteGame"), {}, function() {
  46. return preferred.getPreferredGames();
  47. });
  48. },
  49. unfavorite: function() {
  50. var _this = this;
  51. return $.post(this.get("UnfavoriteGame"), {}, function() {
  52. return preferred.getPreferredGames();
  53. });
  54. },
  55. wantGame: function(callback) {
  56. var _this = this;
  57. return $.post(this.get("WantGame"), {}, function() {
  58. _this.wanted = true;
  59. wanted.getWantedGames();
  60. _this.change();
  61. return callback();
  62. });
  63. },
  64. owner: function() {
  65. return this.get("Owner").Handle;
  66. },
  67. deleted: false,
  68. wanted: false
  69. });
  70. libraries = Backbone.Collection.extend({
  71. model: library,
  72. url: function() {
  73. return preferredGamesUrl;
  74. }
  75. });
  76. preferredGamesView = Backbone.View.extend({
  77. el: "#preferredGames",
  78. initialize: function() {
  79. this.preferredGames = new libraries();
  80. this.preferredGames.bind('reset', this.render, this);
  81. return this.preferredGames.fetch();
  82. },
  83. refresh: function() {
  84. return this.preferredGames.fetch();
  85. },
  86. render: function() {
  87. var _this = this;
  88. $(this.el).empty();
  89. this.preferredGames.each(function(library) {
  90. var view;
  91. view = new preferredGameView({
  92. model: library
  93. });
  94. return $(_this.el).append(view.render().el);
  95. });
  96. if (this.preferredGames.length === 0) {
  97. return $(this.el).html('\
  98. <div class="info" id="showFriends" style="padding-left: 30px">\
  99. Games you don\'t own (that your friends have) will show up here.\
  100. </div>\
  101. ');
  102. }
  103. }
  104. });
  105. preferredGameView = Backbone.View.extend({
  106. tagName: "tr",
  107. className: '',
  108. initialize: function() {
  109. return this.model.bind('change', this.apply, this);
  110. },
  111. apply: function() {
  112. if (this.model.deleted || this.model.wanted) {
  113. return $(this.el).fadeOut();
  114. }
  115. },
  116. events: {
  117. "click .cancel": "notInterested",
  118. "click .request": "wantGame",
  119. "click .favorite": "toggleFavorite"
  120. },
  121. notInterested: function() {
  122. var el;
  123. el = this.el;
  124. return this.model.notInterested(function() {
  125. return $(el).fadeOut();
  126. });
  127. },
  128. wantGame: function() {
  129. var el;
  130. el = this.el;
  131. return this.model.wantGame(function() {
  132. return $(el).fadeOut();
  133. });
  134. },
  135. toggleFavorite: function() {
  136. if (this.model.isFavorited()) {
  137. $(this.el).find(".icon-star").tooltip('hide');
  138. $(this.el).find(".icon-star-empty").tooltip('hide');
  139. return this.model.unfavorite();
  140. } else {
  141. $(this.el).find(".icon-star").tooltip('hide');
  142. $(this.el).find(".icon-star-empty").tooltip('hide');
  143. return this.model.favorite();
  144. }
  145. },
  146. render: function() {
  147. var game, starClass;
  148. starClass = "icon-star-empty";
  149. if (this.model.isFavorited()) {
  150. starClass = "icon-star";
  151. }
  152. game = $.tmpl(this.gameTemplate, {
  153. gameName: this.model.shortName(),
  154. searchString: this.model.reviewUrl(),
  155. owner: this.model.owner(),
  156. starClass: starClass
  157. });
  158. $(this.el).html(game);
  159. game.find(".cancel").tooltip({
  160. "title": "<span style='font-size: 16px'>if you aren't interested in the game, put it into qurantine<span>",
  161. "placement": "top"
  162. });
  163. game.find(".request").tooltip({
  164. "title": "<span style='font-size: 16px'>request the game from " + this.model.owner() + "<span>",
  165. "placement": "top"
  166. });
  167. game.find(".icon-star-empty").tooltip({
  168. "title": "<span style='font-size: 16px'>bring the game to the top of your list<span>",
  169. "placement": "top"
  170. });
  171. game.find(".icon-star").tooltip({
  172. "title": "<span style='font-size: 16px'>bring the game down from its pedestal<span>",
  173. "placement": "top"
  174. });
  175. return this;
  176. },
  177. gameTemplate: '\
  178. <td><a href="${searchString}" target="_blank">${gameName}</a></td>\
  179. <td>${owner}</td>\
  180. <td class="span2">\
  181. <i href="javascript:;" class="favorite ${starClass}" style="cursor: pointer; color: red"></i>\
  182. <i href="javascript:;" class="request icon-arrow-up" style="cursor: pointer"></i>\
  183. <i href="javascript:;" class="cancel icon-arrow-down" style="cursor: pointer"></i>\
  184. </td>\
  185. '
  186. });
  187. }).call(this);