/web-app/js/dashboard.js

https://github.com/glenasmith/shameless · JavaScript · 288 lines · 193 code · 80 blank · 15 comment · 3 complexity · 8dc699e4ae5f4a64edfab3f10b7add38 MD5 · raw file

  1. $(function() {
  2. // Model
  3. var Meal = Backbone.Model.extend({
  4. url: function() {
  5. return this.id ? POST.api + '/' + this.id : POST.api;
  6. },
  7. defaults: {
  8. status: '',
  9. longitude: '',
  10. latitude: ''
  11. },
  12. initialize: function() {
  13. console.log("Read Meal: " + this.get("status"));
  14. }
  15. });
  16. // Collection
  17. var MealCollection = Backbone.Collection.extend({
  18. model: Meal,
  19. url: POST.api
  20. });
  21. //View
  22. var MealView = Backbone.View.extend({
  23. tagName: "tr",
  24. template: Handlebars.compile( $("#rowTemplate").html() ),
  25. events: {
  26. "click .delete": "deleteMeal",
  27. "click .edit" : "editMeal"
  28. },
  29. initialize: function () {
  30. _.bindAll(this, "render", "deleteMeal", "editMeal");
  31. },
  32. render: function () {
  33. //var source = $("#rowTemplate").html();
  34. //var template = Handlebars.compile(source);
  35. $(this.el).append(this.template(this.model.toJSON()));
  36. return this;
  37. },
  38. deleteMeal: function () {
  39. var answer = confirm("Are you sure you want to delete this entry? This cannot be undone.");
  40. if (answer) {
  41. this.model.destroy();
  42. }
  43. },
  44. editMeal: function() {
  45. // populate the dialog values
  46. $("#status").val(this.model.get("status"));
  47. // bind the model to the dialog
  48. $( "#dialog-form" ).data("model", this.model);
  49. $( "#dialog-form" ).dialog({
  50. autoOpen: false,
  51. height: 300,
  52. width: 350,
  53. modal: true,
  54. buttons: {
  55. "Update": function() {
  56. var model = $( "#dialog-form" ).data("model");
  57. var newStatus = $("#status").val();
  58. model.set({status: newStatus });
  59. model.save();
  60. $( this ).dialog( "close" );
  61. },
  62. Cancel: function() {
  63. $( this ).dialog( "close" );
  64. }
  65. }
  66. });
  67. $( "#dialog-form" ).dialog( "open" );
  68. }
  69. });
  70. var MealCollectionView = Backbone.View.extend({
  71. events: {
  72. "click #bbRefresh": "add"
  73. },
  74. initialize: function() {
  75. _.bindAll(this, 'render', 'add');
  76. this.collection.bind('all', this.render);
  77. },
  78. render: function() {
  79. $(this.el).html("<table id='bbFoods'/>"); // create holder for the view, blanking existing content
  80. var bbFoods = $("#bbFoods");
  81. this.collection.each(function (onePost) {
  82. var nextMeal = new MealView({model: onePost});
  83. var renderedPost = nextMeal.render().el
  84. bbFoods.append(renderedPost);
  85. });
  86. return this;
  87. },
  88. remove: function() {
  89. $(this.el).remove();
  90. },
  91. add: function() {
  92. $("#new-zone-form-dialog").dialog("open");
  93. }
  94. });
  95. var ToolbarView = Backbone.View.extend({
  96. template: Handlebars.compile( $("#mainToolbarTemplate").html() ),
  97. events: {
  98. "click #addEntry": "add"
  99. },
  100. initialize: function() {
  101. _.bindAll(this, 'render', 'add');
  102. this.collection.bind('all', this.render);
  103. },
  104. render: function() {
  105. $(this.el).html(""); // wipe existing
  106. $(this.el).append(this.template([]));
  107. return this;
  108. },
  109. add: function() {
  110. // blank the dialog values
  111. $("#status").val("");
  112. // bind the model to the dialog
  113. $( "#dialog-form" ).data("collection", this.collection);
  114. $( "#dialog-form" ).dialog({
  115. autoOpen: false,
  116. height: 300,
  117. width: 350,
  118. modal: true,
  119. buttons: {
  120. "Create": function() {
  121. var collection = $( "#dialog-form" ).data("collection");
  122. var newStatus = $("#status").val();
  123. var meal = new Meal;
  124. meal.set({status: newStatus});
  125. meal.save();
  126. collection.add(meal);
  127. $( this ).dialog( "close" );
  128. },
  129. Cancel: function() {
  130. $( this ).dialog( "close" );
  131. }
  132. }
  133. });
  134. $( "#dialog-form" ).dialog( "open" );
  135. }
  136. });
  137. var DetailToolbarView = Backbone.View.extend({
  138. template: Handlebars.compile( $("#detailToolbarTemplate").html() ),
  139. events: {
  140. "click #closeEntry": "close"
  141. },
  142. initialize: function() {
  143. _.bindAll(this, 'render', 'close');
  144. //this.collection.bind('all', this.render);
  145. },
  146. render: function() {
  147. $(this.el).html(""); // wipe existing
  148. $(this.el).append(this.template([]));
  149. return this;
  150. },
  151. close: function() {
  152. // nav back to the home page
  153. postApp.navigate("", true);
  154. }
  155. });
  156. var DetailView = Backbone.View.extend({
  157. template: Handlebars.compile( $("#detailTemplate").html() ),
  158. events: {
  159. "click #bbRefresh": "add"
  160. },
  161. initialize: function() {
  162. _.bindAll(this, 'render', 'add');
  163. },
  164. render: function() {
  165. $(this.el).html(""); // wipe existing
  166. $(this.el).append(this.template(this.model.toJSON()));
  167. return this;
  168. },
  169. remove: function() {
  170. $(this.el).remove();
  171. },
  172. add: function() {
  173. $("#new-zone-form-dialog").dialog("open");
  174. }
  175. });
  176. // Controller
  177. var MealRouter = Backbone.Router.extend({
  178. routes: {
  179. "": "allMeals",
  180. "meal/:id": "oneMeal"
  181. },
  182. initialize: function (options) {
  183. },
  184. mealList: new MealCollection,
  185. allMeals: function () {
  186. var view = new MealCollectionView({ collection: this.mealList, el: $("#bbBody") });
  187. var toolbar = new ToolbarView({ collection: this.mealList, el: $("#bbToolbar") });
  188. this.mealList.fetch();
  189. },
  190. oneMeal: function (id) {
  191. var detail = this.mealList.get(id)
  192. if (!detail) {
  193. detail = new Meal({"id": id});
  194. detail.fetch({async: false});
  195. } else {
  196. // we fetched from the cache...
  197. }
  198. var view = new DetailView({ model: detail, el: $("#bbBody") });
  199. view.render();
  200. var toolbar = new DetailToolbarView({ el: $("#bbToolbar") });
  201. toolbar.render();
  202. }
  203. });
  204. // Start the backbone app
  205. var postApp = new MealRouter;
  206. // And start all the magic....
  207. Backbone.history.start();
  208. });