/js/coffee/helloworld4.js
JavaScript | 67 lines | 61 code | 5 blank | 1 comment | 0 complexity | e898dd8205ff1ab7c97dd33e7e84fef4 MD5 | raw file
- // Generated by CoffeeScript 1.3.3
- (function() {
- window.Item = Backbone.Model.extend({
- defaults: {
- part1: "hello",
- part2: "world"
- }
- });
- window.List = Backbone.Collection.extend({
- model: Item
- });
- window.ItemView = Backbone.View.extend({
- tagName: "li",
- initialize: function() {
- return _.bindAll(this);
- },
- render: function() {
- this.$el.html(_.template($("#template_helloworld_item").html(), {
- part1: this.model.get("part1"),
- part2: this.model.get("part2")
- }));
- return this;
- }
- });
- window.ListView = Backbone.View.extend({
- events: {
- "click button#add": "addItem"
- },
- initialize: function() {
- _.bindAll(this);
- this.collection = new List();
- this.collection.bind("add", this.appendItem);
- this.counter = 0;
- return this.render();
- },
- render: function() {
- var self;
- self = this;
- this.$el.html(_.template($("#template_helloworld2").html(), {}));
- this.collection.each(function(item) {
- return self.appendItem(item);
- }, this);
- return $(".backbone").html(this.$el);
- },
- addItem: function() {
- var item;
- this.counter++;
- item = new Item();
- item.set({
- part2: item.get("part2") + this.counter
- });
- return this.collection.add(item);
- },
- appendItem: function(item) {
- var itemView;
- itemView = new ItemView({
- model: item
- });
- return $("ul", this.$el).append(itemView.render().$el);
- }
- });
- }).call(this);