PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/js/coffee/helloworld4.js

https://bitbucket.org/digitaltrike/backbonejs-hello-world
JavaScript | 67 lines | 61 code | 5 blank | 1 comment | 0 complexity | e898dd8205ff1ab7c97dd33e7e84fef4 MD5 | raw file
  1. // Generated by CoffeeScript 1.3.3
  2. (function() {
  3. window.Item = Backbone.Model.extend({
  4. defaults: {
  5. part1: "hello",
  6. part2: "world"
  7. }
  8. });
  9. window.List = Backbone.Collection.extend({
  10. model: Item
  11. });
  12. window.ItemView = Backbone.View.extend({
  13. tagName: "li",
  14. initialize: function() {
  15. return _.bindAll(this);
  16. },
  17. render: function() {
  18. this.$el.html(_.template($("#template_helloworld_item").html(), {
  19. part1: this.model.get("part1"),
  20. part2: this.model.get("part2")
  21. }));
  22. return this;
  23. }
  24. });
  25. window.ListView = Backbone.View.extend({
  26. events: {
  27. "click button#add": "addItem"
  28. },
  29. initialize: function() {
  30. _.bindAll(this);
  31. this.collection = new List();
  32. this.collection.bind("add", this.appendItem);
  33. this.counter = 0;
  34. return this.render();
  35. },
  36. render: function() {
  37. var self;
  38. self = this;
  39. this.$el.html(_.template($("#template_helloworld2").html(), {}));
  40. this.collection.each(function(item) {
  41. return self.appendItem(item);
  42. }, this);
  43. return $(".backbone").html(this.$el);
  44. },
  45. addItem: function() {
  46. var item;
  47. this.counter++;
  48. item = new Item();
  49. item.set({
  50. part2: item.get("part2") + this.counter
  51. });
  52. return this.collection.add(item);
  53. },
  54. appendItem: function(item) {
  55. var itemView;
  56. itemView = new ItemView({
  57. model: item
  58. });
  59. return $("ul", this.$el).append(itemView.render().$el);
  60. }
  61. });
  62. }).call(this);