/js/coffee/helloworld5.js
JavaScript | 91 lines | 84 code | 6 blank | 1 comment | 0 complexity | 360085bbfc2cdbc391324f2719cbd7a0 MD5 | raw file
- // Generated by CoffeeScript 1.3.3
- (function() {
- Backbone.sync = function(method, model, success, error) {
- return success();
- };
- window.Item = Backbone.Model.extend({
- defaults: {
- part1: "hello",
- part2: "world"
- }
- });
- window.List = Backbone.Collection.extend({
- model: Item
- });
- window.ItemView = Backbone.View.extend({
- tagName: "li",
- events: {
- "click span.swap": "swap",
- "click span.delete": "remove"
- },
- initialize: function() {
- _.bindAll(this);
- this.model.bind("change", this.render);
- return this.model.bind("remove", this.unrender);
- },
- render: function() {
- this.$el.html(_.template($("#template_helloworld_item2").html(), {
- part1: this.model.get("part1"),
- part2: this.model.get("part2")
- }));
- return this;
- },
- unrender: function() {
- return this.$el.remove();
- },
- swap: function() {
- var swapped;
- swapped = {
- part1: this.model.get("part2"),
- part2: this.model.get("part1")
- };
- return this.model.set(swapped);
- },
- remove: function() {
- return this.model.destroy();
- }
- });
- 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);