PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/js/coffee/helloworld3.js

https://bitbucket.org/digitaltrike/backbonejs-hello-world
JavaScript | 49 lines | 44 code | 4 blank | 1 comment | 0 complexity | cab19fc56f0c42fb526ee8d4a634fc6f 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.ListView = Backbone.View.extend({
  13. events: {
  14. "click button#add": "addItem"
  15. },
  16. initialize: function() {
  17. _.bindAll(this);
  18. this.collection = new List();
  19. this.collection.bind("add", this.appendItem);
  20. this.counter = 0;
  21. return this.render();
  22. },
  23. render: function() {
  24. var self;
  25. self = this;
  26. this.$el.html(_.template($("#template_helloworld2").html(), {}));
  27. this.collection.each(function(item) {
  28. return self.appendItem(item);
  29. }, this);
  30. return $(".backbone").html(this.$el);
  31. },
  32. addItem: function() {
  33. var item;
  34. this.counter++;
  35. item = new Item();
  36. item.set({
  37. part2: item.get("part2") + this.counter
  38. });
  39. return this.collection.add(item);
  40. },
  41. appendItem: function(item) {
  42. return $("ul", this.$el).append("<li>" + item.get("part1") + " " + item.get("part2") + "</li>");
  43. }
  44. });
  45. }).call(this);