PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/VidPub.Web/Public/javascripts/production-admin.js

http://github.com/tekpub/mvc3
JavaScript | 298 lines | 272 code | 17 blank | 9 comment | 3 complexity | 810aa4764388399b2788c873336c652c MD5 | raw file
  1. Episode = Backbone.Model.extend({
  2. defaults : {
  3. Title: "New Episode",
  4. Description : "Lorem Ipsum",
  5. ReleasedOn : "12/31/2011"
  6. },
  7. initialize: function () {
  8. this.bind("error", this.notifyCollectionError);
  9. this.bind("change", this.notifyCollectionChange);
  10. },
  11. idAttribute: "ID",
  12. url: function () {
  13. return this.isNew() ? "/api/episodes/create" : "/api/episodes/edit/" + this.get("ID");
  14. }
  15. });
  16. Episodes = Backbone.Collection.extend({
  17. model: Episode,
  18. url: function () {
  19. return "/api/episodes/?pid=" + this.ProductionID;
  20. }
  21. });
  22. Production = Backbone.Model.extend({
  23. defaults: {
  24. Title: "New Production",
  25. Author: "Joe Blow",
  26. Price: "25.00",
  27. Description: "Lorem Ipsum",
  28. ReleasedOn: "12/31/2011"
  29. },
  30. initialize: function () {
  31. this.bind("error", this.notifyCollectionError);
  32. this.bind("change", this.notifyCollectionChange);
  33. },
  34. idAttribute: "ID",
  35. url: function () {
  36. return this.isNew() ? "/api/productions/create" : "/api/productions/edit/" + this.get("ID");
  37. },
  38. validate: function (atts) {
  39. if ("Title" in atts & !atts.Title) {
  40. return "Title is required";
  41. }
  42. },
  43. notifyCollectionError: function (model, error) {
  44. this.collection.trigger("itemError", error);
  45. },
  46. notifyCollectionChange: function () {
  47. this.collection.trigger("itemChanged", this);
  48. }
  49. });
  50. Productions = Backbone.Collection.extend({
  51. model : Production,
  52. url : "/api/productions"
  53. });
  54. productions = new Productions();
  55. ListView = Backbone.View.extend({
  56. tagName: "ol",
  57. initialize: function () {
  58. _.bindAll(this, 'render');
  59. this.template = $("#listTemplate");
  60. this.collection.bind("itemSaved", this.render);
  61. },
  62. events: {
  63. "click .production-link": "showForm",
  64. "click #new-production": "showCreate"
  65. },
  66. showCreate: function () {
  67. app.navigate("create", true);
  68. return false;
  69. },
  70. showForm: function (evt) {
  71. //get the ID that was clicked
  72. var id = $(evt.currentTarget).data('production-id');
  73. //navigate
  74. app.navigate("edit/" + id, true);
  75. return false;
  76. },
  77. render: function () {
  78. var data = { items: this.collection.toJSON() };
  79. var html = this.template.tmpl(data);
  80. $(this.el).html(html);
  81. return this;
  82. }
  83. });
  84. FormView = Backbone.View.extend({
  85. initialize: function () {
  86. _.bindAll(this, "render");
  87. this.template = $("#productionFormTemplate");
  88. },
  89. events: {
  90. "change input": "updateModel",
  91. "submit #productionForm": "save"
  92. },
  93. save: function () {
  94. this.model.save(
  95. this.model.attributes,
  96. {
  97. success: function (model, response) {
  98. model.collection.trigger("itemSaved", model);
  99. },
  100. error: function (model, response) {
  101. model.trigger("itemError", "There was a problem saving " + model.get("Title"));
  102. }
  103. }
  104. );
  105. return false;
  106. },
  107. updateModel: function (evt) {
  108. var field = $(evt.currentTarget);
  109. var data = {};
  110. var key = field.attr('ID');
  111. var val = field.val();
  112. data[key] = val;
  113. if (!this.model.set(data)) {
  114. //reset the form field
  115. field.val(this.model.get(key));
  116. }
  117. },
  118. render: function () {
  119. var html = this.template.tmpl(this.model.toJSON());
  120. $(this.el).html(html);
  121. this.$(".datepicker").datepicker();
  122. return this;
  123. }
  124. });
  125. NotifierView = Backbone.View.extend({
  126. initialize: function () {
  127. this.template = $("#notifierTemplate");
  128. this.className = "success";
  129. this.message = "Success";
  130. _.bindAll(this, "render", "notifySave", "notifyError");
  131. //use the globals - no need to depend on a single collection
  132. productions.bind("itemSaved", this.notifySave);
  133. productions.bind("itemError", this.notifyError);
  134. },
  135. events: {
  136. "click": "goAway"
  137. },
  138. goAway: function () {
  139. $(this.el).delay(3000).fadeOut();
  140. },
  141. notifySave: function (model) {
  142. this.message = model.get("Title") + " saved";
  143. this.render();
  144. this.goAway();
  145. },
  146. notifyError: function (message) {
  147. this.message = message;
  148. this.className = "error";
  149. this.render();
  150. this.goAway();
  151. },
  152. render: function () {
  153. var html = this.template.tmpl({ message: this.message, className: this.className });
  154. $(this.el).show();
  155. $(this.el).html(html);
  156. return this;
  157. }
  158. });
  159. EpisodeFormView = Backbone.View.extend({
  160. initialize: function () {
  161. this.template = $("#episodeFormTemplate");
  162. },
  163. events: {
  164. "submit #episodeForm": "save",
  165. "change input": "updateModel"
  166. },
  167. save: function (evt) {
  168. this.model.save(
  169. this.model.attributes,
  170. {
  171. success: function (model, response) {
  172. alert(model.get("Title") + " saved");
  173. },
  174. error: function (model, response) {
  175. alert("Problems... " + response.responseText);
  176. }
  177. }
  178. );
  179. return false;
  180. },
  181. updateModel: function (evt) {
  182. var field = $(evt.currentTarget);
  183. var data = {};
  184. var key = field.attr('ID');
  185. var val = field.val();
  186. data[key] = val;
  187. if (!this.model.set(data)) {
  188. //reset the form field
  189. field.val(this.model.get(key));
  190. }
  191. },
  192. render: function () {
  193. var html = this.template.tmpl(this.model.toJSON());
  194. $(this.el).html(html);
  195. return this;
  196. }
  197. });
  198. EpisodeListView = Backbone.View.extend({
  199. initialize: function () {
  200. this.template = $("#episodeListTemplate");
  201. _.bindAll(this, "render");
  202. },
  203. events: {
  204. "click .episode-link": "editEpisode",
  205. "click #new-episode" : "newEpisode"
  206. },
  207. editEpisode: function (evt) {
  208. var id = $(evt.currentTarget).data('episode-id');
  209. var model = this.collection.get(id);
  210. episodeForm = new EpisodeFormView({ model: model, el: "#episodeForm" });
  211. episodeForm.render();
  212. return false;
  213. },
  214. newEpisode : function(){
  215. episodeForm = new EpisodeFormView({ model: new Episode(), el: "#episodeForm" });
  216. episodeForm.render();
  217. return false;
  218. },
  219. render: function () {
  220. var data = { items: this.collection.toJSON() };
  221. var html = this.template.tmpl(data);
  222. $(this.el).show();
  223. $(this.el).html(html);
  224. return this;
  225. }
  226. });
  227. var ProductionAdmin = Backbone.Router.extend({
  228. initialize: function () {
  229. listView = new ListView({ collection: productions, el: "#production-list" });
  230. formView = new FormView({ el: "#production-form" });
  231. notifierView = new NotifierView({ el: "#notifications" });
  232. },
  233. routes: {
  234. "": "index",
  235. "edit/:id": "edit",
  236. "create": "create"
  237. },
  238. index: function () {
  239. listView.render();
  240. },
  241. edit: function (id) {
  242. listView.render();
  243. $(notifierView.el).empty();
  244. $(formView.el).empty();
  245. //grab the model from the productions
  246. var model = productions.get(id);
  247. formView.model = model;
  248. formView.render();
  249. //grab the episodes
  250. episodes = new Episodes();
  251. episodes.ProductionID = id;
  252. episodes.fetch({
  253. success: function (model, response) {
  254. episodeList = new EpisodeListView({ collection: episodes, el: "#episode-list" });
  255. episodeList.render();
  256. }
  257. });
  258. },
  259. create: function () {
  260. var model = new Production();
  261. listView.render();
  262. $(notifierView.el).empty();
  263. $(formView.el).empty();
  264. $("#episode-list").empty();
  265. $("#episodeForm").empty();
  266. formView.model = model;
  267. formView.render();
  268. }
  269. });
  270. jQuery(function () {
  271. productions.fetch({
  272. success: function () {
  273. //create the router
  274. window.app = new ProductionAdmin();
  275. Backbone.history.start();
  276. },
  277. error: function () {
  278. //display a nice error page
  279. }
  280. });
  281. });