2013-04-23 45 views
0

我正在加載使用骨幹網頁的所有帖子。並在點擊「獲取所有評論」鏈接時加載評論。我收到來自Ajax調用的所有評論。Backbone.js - 從視圖渲染數組,無需在模板中創建循環

Social.Views.StreamsIndex = Backbone.View.extend({ 

    comment_template: JST['streams/comment'], 

    comment_displayall: function(data, post_id) { 
     this.$("#comments").html(this.comment_template({ 
     comment: data // Here data is array 
     })); 
    } 
}); 

comment.jst.ejs文件現在有一個循環,但我不得不把它放在視圖

<% _.each(comment.comments,function(comment){ %> // I want to get rid of this Line 
<div class="stream_comment"> 
    <div class="stream_commentphoto"> 
    <a><img src="<%= comment.actor.thumbnail.url %>"></a> 
    </div> 
    <div class="stream_comm-content"> 
    <a href="#"><h5><%= comment.actor.displayName %></h5> </a> 
    <p><%= comment.content %></p> 
    </div> 
</div> 
<%}%> 

我怎樣才能擺脫循環的評論模板中,通過在視圖中添加循環?

回答

1

也許是這樣的:

comment_displayall: function(data, post_id) { 

    //clear any existing comments 
    var $comments = this.$("#comments").empty(); 

    //render each comment separately and append to the view 
    _.each(data.comments, function(comment) { 
     $comments.append(this.comment_template({comment:comment});  
    }, this); 
} 

而簡單地刪除模板的循環結構(第一和最後一行)。

/代碼示例沒有測試

+0

謝謝..這工作.. .. – 2013-04-23 10:55:55