2014-03-12 12 views
0

我對集合中的模型進行了循環以使用集合描述中的函數fetchAll進行提取。骨幹 - 如何在獲取集合中的所有模型後觸發呈現視圖

fetchAll: function(){ 
    this.counter=0; 
    self = this; 
     for (var i=0;i<this.models.length; i++){ 
     this.models[i].fetch({ 
     success: function(){ 
      self.counter +=1; 
      if (self.counter == self.models.length){ 
      alert('done'); 
      self.doneFetchAll = true; 
      } 
     } 
     }); 

     //console.log(i); 
     } 

的抓取完成後,我看到一個警告和集合屬性doneFetchAll設置爲true ....但如何觸發渲染視圖的已完成這項工作之後?

1)是否有骨幹聽取集合中某個特定屬性變化的可能性,如果是肯定的,再次調用渲染?

OR

2)是否有更好的方法來獲取集合中的所有模型,然後重新渲染視圖?

所有聽的變化這一努力已經失敗(鱈視圖的initialize: function()):

this.listenTo(this.collection, "change:doneFetchAll", this.render); 

this.collection.on("change:doneFetchAll", this.render, this); 

感謝。

回答

1

嘗試使用自定義事件:

fetchAll: function(){ 
    this.counter=0; 
    self = this; 
     for (var i=0;i<this.models.length; i++){ 
     this.models[i].fetch({ 
     success: function(){ 
      self.counter +=1; 
      if (self.counter == self.models.length){ 
      self.trigger('fetchAll'); // here 
      self.doneFetchAll = true; 
      } 
     } 
     }); 

    //console.log(i); 
    } 

然後在initialize: function()

this.listenTo(this.collection, "fetchAll", this.render); 
0

廣東話你只聽 「同步」 事件,而不是使用fetchall?

this.listenTo(this.collection, "sync", this.render)?