2012-07-20 40 views
1

根據Composite View documentation在 複合視圖中事件reset,removeadd已經綁定到集合。複合視圖綁定的初始事件

說,爲什麼我需要綁定reset事件來渲染我的CompositeView?

PS:
我使用Backbone.Marionette v0.9.1
請詳見(1)的代碼,(2)
其實這個問題是關於serializeData, 因爲當渲染功能從initialEvents變量has_message叫設置爲零。所以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}} 

回答