2017-06-07 3 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が完成する前にテンプレートがレンダリングされています。

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

:あなたは fetchsuccessコールバックでレンダリングコードを呼び出す必要があります
関連する問題