PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/js/recipe_app.js

https://bitbucket.org/citadelgrad/recipes-app
JavaScript | 267 lines | 178 code | 55 blank | 34 comment | 0 complexity | 2746dd525a36d6c1a8c356938e433a40 MD5 | raw file
  1. (function($) {
  2. var HHShop = {
  3. view: {},
  4. model: {},
  5. collection: {},
  6. };
  7. /* ALBUM MODEL */
  8. window.Recipe = Backbone.Model.extend({});
  9. HHShop.model.ShoppingList = Backbone.Model.extend({});
  10. /* ALBUMS COLLECTION */
  11. window.Recipes = Backbone.Collection.extend({
  12. /* Set the url that we can later fetch the json data.
  13. It is currently done on the onload event in the html.
  14. */
  15. model: Recipe,
  16. url: "recipes.json"
  17. });
  18. HHShop.collection.ShoppingLists = Backbone.Collection.extend({
  19. model: HHShop.model.ShoppingList
  20. });
  21. //rec = new window.Recipes();
  22. //window.stuff = rec.fetch();
  23. window.library = new Recipes();
  24. HHShop.shoppingList = new HHShop.collection.ShoppingLists();
  25. $(document).ready(function() {
  26. /* RecipeLibrary view is the main view for creating the list
  27. of recipe names.
  28. */
  29. window.RecipeLibraryView = Backbone.View.extend({
  30. template: _.template($("#recipelist-template").html()),
  31. tag: 'li',
  32. className: 'recipelibrary',
  33. events: {
  34. 'click a.shoppinglist': 'shoplist'
  35. },
  36. initialize: function() {
  37. _.bindAll(this, 'render', 'shoplist');
  38. /* The reset event calls the render method that fetches the
  39. data and allows it to be displayed.
  40. */
  41. this.collection.bind('reset', this.render);
  42. //this.collectoin.bind('click', 'shoplist');
  43. this.router = this.options.router;
  44. console.log(this.router);
  45. },
  46. shoplist: function() {
  47. console.log('click and funcition fired')
  48. this.router.routes("shopping_list", true);
  49. },
  50. render: function() {
  51. var $recipes,
  52. collection = this.collection;
  53. $(this.el).html(this.template({})); // This render the View's template
  54. /* Scopes to current element so that we can then add a new li
  55. for each recipe.
  56. */
  57. $recipes = this.$(".recipes");
  58. this.collection.each(function(recipe) {
  59. /* Applies the View inside of this function and then appends
  60. the result.
  61. */
  62. var view = new RecipeListItemView({
  63. model: recipe,
  64. collection: collection
  65. });
  66. $recipes.append(view.render().el);
  67. });
  68. return this;
  69. }
  70. });
  71. window.RecipeListItemView = RecipeLibraryView.extend({
  72. template: _.template($("#recipeitem-template").html()),
  73. tag: 'li',
  74. className: 'recipes',
  75. events: {
  76. 'click .recipe-title': 'showRecipe'
  77. },
  78. initialize: function() {
  79. _.bindAll(this, 'render', 'showRecipe');
  80. this.router = this.options.router;
  81. },
  82. showRecipe: function(id) {
  83. /* This can probably be done by creating a function that calls
  84. another view as it is done above with the render method.
  85. */
  86. var view = new RecipeDetailView({
  87. model: this.model,
  88. collection: this.collection
  89. });
  90. //$("#container").append("<div id='recipedetail'></div>");
  91. $(this.el).append(view.render().el);
  92. console.log( "Get post number " + this.model.id);
  93. this.router.navigate("recipe/"+ this.model.id, true);
  94. // window.library.get(id=7)
  95. return this;
  96. },
  97. render: function() {
  98. $(this.el).html(this.template(this.model.toJSON()));
  99. return this;
  100. }
  101. });
  102. window.RecipePaneView = Backbone.View.extend({
  103. template: _.template($("#recipepane-template").html()),
  104. className: 'recipepane',
  105. tag: 'div',
  106. initialize: function() {
  107. _.bindAll(this, 'render');
  108. this.collection.bind('reset', this.render);
  109. //this.collection.bind('click', this.render);
  110. },
  111. render: function() {
  112. var $details,
  113. collection = this.collection;
  114. var view = new RecipePaneView({
  115. model: this.model,
  116. collection: collection
  117. });
  118. $(this.el).html(view.render().el);
  119. return this;
  120. }
  121. });
  122. window.RecipeDetailView = Backbone.View.extend({
  123. template: _.template($("#recipedetail-template").html()),
  124. className: "detail-panel",
  125. tag: 'div',
  126. initialize: function() {
  127. _.bindAll(this, 'render');
  128. this.collection.bind('reset', this.render);
  129. /* The 'click' event must be in the init so that when the
  130. related event fires it executes the render method.
  131. */
  132. this.collection.bind('click', this.render);
  133. collection = this.collection;
  134. var view = new RecipePaneView({
  135. model: this.model,
  136. collection: collection
  137. });
  138. },
  139. render: function() {
  140. $("#details-pane").empty();
  141. $("#details-pane").html(this.template(this.model.toJSON()));
  142. return this;
  143. }
  144. });
  145. HHShop.view.ShoppingList = Backbone.View.extend({
  146. el: '#details-pane',
  147. template: _.template($("#shoplist-template").html()),
  148. initialize: function () {
  149. console.log('load ShoppingList');
  150. _.bindAll(this, 'render');
  151. this.collection.bind('reset', this.render);
  152. var view = new HHShop.view.ShopListItem({
  153. model: this.model,
  154. collection: this.collection
  155. })
  156. },
  157. render: function() {
  158. $("#details-pane").empty();
  159. $("#details-pane").html(this.template(this.model.toJSON()));
  160. return this;
  161. }
  162. });
  163. HHShop.view.ShopListItem = Backbone.View.extend({
  164. template: _.template($("#shoplistitem-template").html()),
  165. tag: 'li',
  166. initialize: function() {
  167. _.bindAll(this, 'render');
  168. },
  169. render: function() {
  170. $(this.el).html("<p>All your base</p>");
  171. console.log("ShoppingList");
  172. }
  173. });
  174. /***** ROUTER ******/
  175. HHShop.recipesRouter = Backbone.Router.extend({
  176. routes: {
  177. '': 'home',
  178. 'blank': 'blank',
  179. 'recipe/:id': 'blank',
  180. 'shopping_list': 'shoppinglist'
  181. },
  182. initialize: function () {
  183. /* Should instaiate the root level view.
  184. */
  185. this.recipeLibraryView = new RecipeLibraryView({
  186. router: this,
  187. collection: window.library
  188. });
  189. this.shoppingListView = new HHShop.view.ShoppingList({
  190. router: this,
  191. collection: HHShop.shoppingList
  192. });
  193. },
  194. home: function() {
  195. $('#container').empty();
  196. $("#container").append(this.recipeLibraryView.render().el);
  197. },
  198. shoppinglist: function() {
  199. $('#details-pane').empty();
  200. $('#details-pane').append(this.shoppingListView.render.el);
  201. },
  202. blank: function() {
  203. $('#container').empty();
  204. $('#container').append("Hello World.");
  205. }
  206. });
  207. // Kick off the application
  208. window.App = new HHShop.recipesRouter();
  209. Backbone.history.start();
  210. });
  211. })(jQuery);