2012-07-20 2 views
1

컴포지트보기의 Composite View documentation에 따르면 reset, removeadd 이벤트가 이미 컬렉션에 바인딩되어 있습니다.컴포지트보기가 ​​바인딩되는 초기 이벤트

왜 CompositeView 렌더링을 위해 reset 이벤트를 바인드해야합니까?

PS :
나는 Backbone.Marionette v0.9.1
더 세부 사항 (1)의 코드를 참조하십시오 사용하고 (2)
사실 문제는 serializeData, 에 관한 기능이 initialEvents 변수 has_message에서 호출 렌더링 할 때 때문에 0으로 설정됩니다. 따라서 ul.messages은 템플릿에 정의되어 있지 않습니다. 어떻게 수정해야합니까?


(1)

var CompositeView = Marionette.CompositeView.extend({ 

    template: CompositeTemplate, 

    itemView: messageView, 

    initialize: function() { 
     this.collection = new MessageCollection(); 
     this.collection.fetch(); 

     this.bindTo(this.collection, 'reset', this.render); 
     // deleting the previous line 
     // I cannot see the collection rendered after the fetch. 
    }, 

    serializeData: function() { 
     return { 
      has_messages: this.collection.length > 0 
     }; 
    }, 


    appendHtml: function (collectionView, itemView) { 
     collectionView.$el.find('ul.messages').append(itemView.el); 
    } 

}); 

복합보기 컬렉션에 리셋 이벤트를 결합하기 때문에 (2)

// Template 


{{#if has_messages }} 
    <!-- list messages --> 
    <ul class="list messages"></ul> 
{{else}} 
    no messages 
{{/if}} 

답변

2

"renset"를 구속 할 필요가 자신은 전체 합성보기가 아닌 렌더링입니다.

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.compositeview.md#events-and-callbacks

serializeData 함수의 문제는 컨텍스트 문제로 인한 것일 수 있습니다. 당신은 당신의 사건이 바인딩 변경해야

this.bindTo(this.collection, 'reset', this.render, this);

이벤트와 뷰의 컨텍스트를 바인딩합니다.