Backbone(2) 뷰

2033 단어 보기backbone
1. 보기
var UserView = Backbone.View.extend({
   initialize: function{/*.......*/},
   render: function{/*.......*/}
});
// DOM this.el
//el tageName,className,id 。
// ,el div
var UserView = Backbone.View.extend({
    tagName: "span",
    className: "users"
});

assertEqual((new UserView).el.className,"users");

// el, 
var UserView = Backbone.View.extend({
    el: $(".users")
});

// el, tageName,className,id 
var userView = new UserView({id: "followers"});

2. 렌더링 뷰//새 HTML을 사용하여 el을 업데이트합니다.
var TodoView = Backbone.View.extend({
    template: _.template($("#todo-template").html()),

    render: function(){
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    }
})
 
_.template("http://documentcloud.github.com/underscore/#template") 

3. 이벤트 의뢰//이벤트를 엘에 추가하는 간단하고 빠른 방법
var TodoView = Backbone.View.extend({
  events: {
      "change input[type=checkbox]" : "toggleDone",
      "click .destroy" : "clear",
  },

  toggleDone: function(e){/* ... */},
  clear: function(e){/* ... */}
});

//events {"eventType selector": "callback"},selector , , el 

4. 바인딩 및 컨텍스트
// , change , render 
var TodoView=Backbone.View.extend({
   initialize: function(){
      _.bindAll(this, 'render' ,'close'); //_.bindAll (http://documentcloud.github.com/undersocre/#bindAll)
      this.model.bind('change' , this.render);
   },

   close: function(){/* ... */}
});

//_.bindAll  render() close() TodoView 

// , el。 delete 
var TodoView = Backbone.View.extend({
    initialize: function(){
       _.bindAll(this, 'render', 'remove');
       this.model.bind('change', 'this.render');
       this.model.bind('delete', 'this.remove');
    },

    remove: function(){
        $(this.el).remove();
    }
});

좋은 웹페이지 즐겨찾기