PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/js/recipe_namespaced.js

https://bitbucket.org/citadelgrad/recipes-app
JavaScript | 205 lines | 127 code | 38 blank | 40 comment | 0 complexity | bead4100a0ff7f03eed38c7a1fd475fc MD5 | raw file
  1. var HHShop = {
  2. view: {},
  3. model: {},
  4. collection: {},
  5. start: function() {
  6. new HHShop.RecipesRouter();
  7. }
  8. };
  9. /* ALBUM MODEL */
  10. HHShop.model.Recipe = Backbone.Model.extend({});
  11. /*
  12. If you are binding to a Collection this.collection.bind("click", this.callback) then
  13. the context of the 'this' in the callback function is "collection context".
  14. Using this.collection.bind("click", this.callback, this) will give it the context of
  15. the view.
  16. */
  17. /* ALBUMS COLLECTION */
  18. HHShop.collection.Recipes = Backbone.Collection.extend({
  19. /* Set the url that we can later fetch the json data.
  20. It is currently done on the onload event in the html. */
  21. model: HHShop.model.Recipe,
  22. url: "recipes.json"
  23. });
  24. //rec = new HHShop.collection.Recipes();
  25. //window.stuff = rec.fetch();
  26. HHShop.library = new HHShop.collection.Recipes();
  27. /* RecipeLibrary view is the main view for creating the list
  28. of recipe names.
  29. */
  30. console.log("here");
  31. HHShop.view.RecipeLibraryView = Backbone.View.extend({
  32. el: '#container',
  33. template: _.template($("#recipelist-template").html()),
  34. tag: 'li',
  35. className: 'recipelibrary',
  36. initialize: function() {
  37. console.log("inner");
  38. _.bindAll(this, 'render');
  39. this.collection.bind('reset', this.render);
  40. /* When the reset event occurs the callback function this.render method
  41. fetches the data and allows it to be displayed.
  42. When you bind an event it will automatically pass the model and collection
  43. to the callback function.
  44. */
  45. },
  46. render: function() {
  47. var $recipes, collection = this.collection;
  48. $(this.el).html(this.template({})); // This renders the View's template
  49. /* Scopes to current element so that we can then add a new li
  50. for each recipe.
  51. */
  52. $recipes = this.$(".recipes");
  53. this.collection.each(function(recipe) {
  54. /* Applies the View inside of this function and then appends
  55. the result.
  56. */
  57. var view = new HHShop.view.RecipeListItemView({
  58. model: recipe,
  59. collection: collection
  60. });
  61. $recipes.append(view.render().el);
  62. });
  63. return this;
  64. }
  65. });
  66. console.log("after");
  67. HHShop.view.RecipeListItemView = HHShop.view.RecipeLibraryView.extend({
  68. template: _.template($("#recipeitem-template").html()),
  69. tag: 'li',
  70. className: 'recipes',
  71. events: {
  72. 'click .recipe-title': 'showRecipe'
  73. },
  74. initialize: function() {
  75. _.bindAll(this, 'render', 'showRecipe');
  76. },
  77. showRecipe: function() {
  78. /* This can probably be done by creating a function that calls
  79. another view as it is done above with the render method.
  80. */
  81. var view = new HHShop.view.RecipeDetailView({
  82. model: this.model,
  83. collection: this.collection
  84. });
  85. //$("#container").append("<div id='recipedetail'></div>");
  86. $(this.el).append(view.render().el);
  87. // window.library.get(id=7)
  88. return this;
  89. },
  90. render: function() {
  91. $(this.el).html(this.template(this.model.toJSON()));
  92. return this;
  93. }
  94. });
  95. HHShip.view.RecipePaneView = Backbone.View.extend({
  96. template: _.template($("#recipepane-template").html()),
  97. className: 'recipepane',
  98. tag: 'div',
  99. initialize: function() {
  100. _.bindAll(this, 'render');
  101. this.collection.bind('reset', this.render);
  102. //this.collection.bind('click', this.render);
  103. },
  104. render: function() {
  105. var $details, collection = this.collection;
  106. var view = new HHShop.view.RecipePaneView({
  107. model: this.model,
  108. collection: collection
  109. });
  110. $(this.el).html(view.render().el);
  111. return this;
  112. }
  113. });
  114. HHShop.view.RecipeDetailView = Backbone.View.extend({
  115. template: _.template($("#recipedetail-template").html()),
  116. className: "detail-panel",
  117. tag: 'div',
  118. initialize: function() {
  119. _.bindAll(this, 'render');
  120. this.collection.bind('reset', this.render);
  121. /* The 'click' event must be in the init so that when the
  122. related event fires it executes the render method.
  123. */
  124. this.collection.bind('click', this.render);
  125. collection = this.collection;
  126. var view = new HHShop.view.RecipePaneView({
  127. model: this.model,
  128. collection: collection
  129. });
  130. },
  131. render: function() {
  132. $("#details-pane").empty();
  133. $("#details-pane").html(this.template(this.model.toJSON()));
  134. return this;
  135. }
  136. });
  137. /***** ROUTER ******/
  138. HHShop.RecipesRouter = Backbone.Router.extend({
  139. routes: {
  140. '': 'home',
  141. 'blank': 'blank',
  142. 'recipe/:id': 'blank'
  143. },
  144. initialize: function() {
  145. /* Should instaiate the root level view. */
  146. console.log("init Router");
  147. this.recipeLibraryView = new HHShop.view.RecipeLibraryView({
  148. collection: HHShop.library
  149. });
  150. },
  151. home: function() {
  152. $('#container').empty();
  153. $("#container").append(this.recipeLibraryView.render().el);
  154. },
  155. blank: function() {
  156. $('#container').empty();
  157. $('#container').append("Hello World.");
  158. }
  159. });
  160. $(document).ready(function() {
  161. // Kick off the application
  162. //window.App = new BackboneRecipes();
  163. Backbone.history.start({
  164. pushState: true
  165. });
  166. });