zoukankan      html  css  js  c++  java
  • Backbone 学习5

    (function($){
      Backbone.sync = function(method, model, success, error){
        success();
      }
    
      var Item = Backbone.Model.extend({
        defaults: {
          part1: 'hello',
          part2: 'world'
        }
      });
    
      var List = Backbone.Collection.extend({
        model: Item
      });
    
      var ItemView = Backbone.View.extend({
        tagName: 'li', 
        events: {
          'click span.swap':  'swap',
          'click span.delete': 'remove'
        },
        initialize: function(){
          _.bindAll(this, 'render', 'unrender', 'swap', 'remove'); 
          this.model.bind('change', this.render);
          this.model.bind('remove', this.unrender);
        },
        render: function(){
          $(this.el).html('<span style="color:black;">'+this.model.get('part1')+' '+this.model.get('part2')+'</span> &nbsp; &nbsp; <span class="swap" style="font-family:sans-serif; color:blue; cursor:pointer;">[swap]</span> <span class="delete" style="cursor:pointer; color:red; font-family:sans-serif;">[delete]</span>');
          return this; 
        },
        unrender: function(){
          $(this.el).remove();
        },
        swap: function(){
          var swapped = {
            part1: this.model.get('part2'),
            part2: this.model.get('part1')
          };
          this.model.set(swapped);
        },
        remove: function(){
          this.model.destroy();
        }
      });
    
      var ListView = Backbone.View.extend({
        el: $('body'), 
        events: {
          'click button#add': 'addItem'
        },
        initialize: function(){
          _.bindAll(this, 'render', 'addItem', 'appendItem'); 
          this.collection = new List();
          this.collection.bind('add', this.appendItem); 
    
          this.counter = 0;
          this.render();
        },
        render: function(){
          var self = this;
          $(this.el).append("<button id='add'>Add list item</button>");
          $(this.el).append("<ul></ul>");
          _(this.collection.models).each(function(item){ 
            self.appendItem(item);
          }, this);
        },
        addItem: function(){
          this.counter++;
          var item = new Item();
          item.set({
            part2: item.get('part2') + this.counter 
          });
          this.collection.add(item);
        },
        appendItem: function(item){
          var itemView = new ItemView({
            model: item
          });
          $('ul', this.el).append(itemView.render().el);
        }
      });
    
      var listView = new ListView();
    })(jQuery);
  • 相关阅读:
    2017暑期集训Day 4
    2017暑期集训Day 5
    2017暑期集训Day 3
    Codeforces Round #433
    校内集训(20170906)
    校内集训(20170903)
    培训补坑(day10:双指针扫描+矩阵快速幂)
    培训补坑(day8:树上倍增+树链剖分)
    培训补坑(day7:线段树的区间修改与运用)(day6是测试,测试题解以后补坑QAQ)
    培训补坑(day5:最小生成树+负环判断+差分约束)
  • 原文地址:https://www.cnblogs.com/wangwenfei/p/Backbone_el.html
Copyright © 2011-2022 走看看