2013-04-27 113 views
0

我正在通過骨幹網應用程序(對於骨幹很新)我的方式工作,並需要一些幫助迭代通過集合,因爲我拿它。我想要做的是循環集合中的每個元素,如果progress屬性是100顯示導入的<div>,否則顯示加載<div>迭代雖然Backbone.js集合

我有這個工作最後一個元素集合中,如下所示:

fileRepositoryCollection.fetch({ 

    success: function() { 

     if (fileRepositoryCollection.at(fileRepositoryCollection.length - 1).get('progress') === 100) { 
      $('#loading').hide(); 
      $('#imported').show(); 
     } else { 
      $('#loading').show(); 
      $('#imported').hide(); 
     } 
    } 
}); 

回答

1

在集合類你不應該渲染HTML。你的集合視圖應該有一個呈現方法,定義你的集合在html中的樣子。然後,您通過視圖中的集合進行迭代。

事情是這樣的:

var fileRepositoryCollectionView = Backbone.View.extend({ 

    render: function() { 
     // collection rendering 

     this.collection.each(function(file) { 
      if (file.get('progress') === 100) { 
       $('#loading' + file.get('some_id')).hide(); 
       $('#imported' + file.get('some_id')).show(); 
      } else { 
       $('#loading' + file.get('some_id')).show(); 
       $('#imported' + file.get('some_id')).hide(); 
      } 
     }); 

     return this; 
    }, 
}); 

var repView = new fileRepositoryCollectionView({ collection: fileRepositoryCollection}); 
+3

你也可以單獨渲染的一個元素。通常會創建addOne方法。 – 2013-04-27 16:13:34

+0

Thansks的 - 我已經把相關的代碼備份到渲染方法現在,即時通訊使用表結構與圖像顯示取決於進展=== 100或<100,因此我必須保持我的兩個不同圖像在一個TD元素(進口和加載)如下:​​,遺憾的是目前的代碼,我都試過了,你建議的代碼只與最後一個元素現在正在收集中... – user1694873 2013-04-27 16:34:40