PageRenderTime 67ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/articles/compare/javascripts/backbone-src.js

https://github.com/crazytakeharu/jsmvctutorial
JavaScript | 180 lines | 148 code | 32 blank | 0 comment | 5 complexity | 1da5c2ad81116f97825f3c98b793cb2f MD5 | raw file
  1. $(function() {
  2. _.templateSettings = {
  3. interpolate: /\{\{\=(.+?)\}\}/g,
  4. evaluate: /\{\{(.+?)\}\}/g
  5. };
  6. var Beer = Backbone.Model.extend({
  7. defaults:{},
  8. initialize: function() {
  9. }
  10. });
  11. var BeerCollection = Backbone.Collection.extend({
  12. model:Beer,
  13. url:function() {
  14. return "http://tutorials.jeffreyokawa.com/beer/show?filter=" + this.filter
  15. },
  16. parse: function (response) {
  17. var attrs = new Array();
  18. _.each(response, function(beer) {
  19. attrs.push(beer.beer);
  20. });
  21. return attrs;
  22. }
  23. });
  24. var FavoritesCollection = Backbone.Collection.extend({
  25. model:Beer
  26. });
  27. var FavoritesListItemView = Backbone.View.extend({
  28. tagName: "li",
  29. template: _.template($("#favorites-list-item-view").html()),
  30. events:{
  31. "click .close":"removeFavoriteFromList"
  32. },
  33. render: function(event) {
  34. this.$el.html(this.template(this.model.toJSON()));
  35. return this;
  36. },
  37. removeFavoriteFromList: function(){
  38. this.favoritesList.remove(this.model);
  39. }
  40. });
  41. var FavoritesListView = Backbone.View.extend({
  42. tagName:'ul',
  43. initialize:function () {
  44. this.model.bind("reset", this.render, this);
  45. this.model.bind("add", this.render, this);
  46. this.model.bind("remove", this.render, this);
  47. },
  48. render: function (event) {
  49. this.$el.empty();
  50. _.each(this.model.models, function (item) {
  51. var favListItemView = new FavoritesListItemView({model:item});
  52. favListItemView.favoritesList = this.model;
  53. this.$el.append(favListItemView.render().el);
  54. }, this);
  55. return this;
  56. }
  57. });
  58. var BeerFilterView = Backbone.View.extend({
  59. el: $('#beer-filter'),
  60. template: _.template($("#beer-filter-view").html()),
  61. render: function(event) {
  62. this.$el.html(this.template());
  63. return this;
  64. },
  65. events: {
  66. "keyup .filter-input": "change"
  67. },
  68. change: function(event) {
  69. var val = $('.filter-input').val();
  70. if (val.length > 2 || val.length == 0) {
  71. this.collection.filter = val;
  72. this.collection.fetch();
  73. }
  74. }
  75. });
  76. var BeerView = Backbone.View.extend({
  77. el: $('#beer-detail'),
  78. events: {
  79. "click #addToFavoritesBtn": "addToFavorites"
  80. },
  81. render: function(event) {
  82. var compiled_template = _.template($("#beer-view").html());
  83. this.$el.html(compiled_template(this.model.toJSON()));
  84. return this; //recommended as this enables calls to be chained.
  85. },
  86. addToFavorites: function () {
  87. this.favoritesList.add(this.model);
  88. }
  89. });
  90. var BeerListViewItem = Backbone.View.extend({
  91. tagName: "li",
  92. template: _.template($("#beer-list-view").html()),
  93. render: function(event) {
  94. this.$el.html(this.template(this.model.toJSON()));
  95. return this;
  96. }
  97. });
  98. var BeerListView = Backbone.View.extend({
  99. tagName:'ul',
  100. initialize:function () {
  101. this.model.bind("reset", this.render, this);
  102. },
  103. render: function (event) {
  104. this.$el.empty();
  105. _.each(this.model.models, function (item) {
  106. this.$el.append(new BeerListViewItem({model:item}).render().el);
  107. }, this);
  108. return this;
  109. }
  110. });
  111. var Router = Backbone.Router.extend({
  112. beerList:null,
  113. routes:{
  114. "":"index",
  115. "beer/:id":"beerDetails"
  116. },
  117. index:function() {
  118. this.beerList = new BeerCollection();
  119. this.beerList.filter = "";
  120. this.favoritesList = new FavoritesCollection();
  121. var blv = new BeerListView({model:this.beerList});
  122. var router = this;
  123. this.beerList.fetch({success:function(){
  124. $("#beer-list").html(blv.render().el);
  125. var b = new BeerFilterView({collection:router.beerList});
  126. b.render();
  127. var flv = new FavoritesListView({model:router.favoritesList});
  128. $("#favorites-list").html(flv.render().el);
  129. if (router.requestedBeerId){
  130. router.beerDetails(router.requestedBeerId);
  131. }
  132. }});
  133. },
  134. beerDetails:function(id) {
  135. if (this.beerList){
  136. var beer = this.beerList.get(id);
  137. var bv = new BeerView({model:beer});
  138. bv.favoritesList = this.favoritesList;
  139. bv.render();
  140. }else{
  141. this.requestedBeerId=id;
  142. this.index();
  143. }
  144. }
  145. });
  146. var app = new Router();
  147. Backbone.history.start();
  148. });