2017-06-07 12 views
-2

我在我的集​​閤中做了一個抓取,我做了一個console.log(),它工作正常,但是當我設置爲我的模板時不顯示值。在骨幹上取指不填充我的模板

在我看來是:

 var reposCollection = new Sice.Collections.RepositoryList(); 
     reposCollection.fetch(); 
     this.$el.html(this.template({collection: reposCollection.models})); 

我的模板是:

 <% _.each(collection, function(repos) { %> 
     <tr>   
      <td><%= repos.attributes.name %></td> 
      <td><%= repos.attributes.description %></td> 
      <td><%= repos.attributes.language %></td> 
     </tr> 
    <% }); %> 

我不知道發生了什麼事!

回答

2

在您的fetch完成之前,您的模板正在渲染。你需要調用渲染代碼在success回調fetch

var reposCollection = new Sice.Collections.RepositoryList(); 
reposCollection.fetch({ 
    success: (collection, response, options) => { 
     this.$el.html(this.template({collection: collection.models})); 
    } 
}); 

Backbone.Collection.prototype.fetch documentation