我有三種不同的視圖模板:「發佈」,「評論」,「添加新評論」。主要模板是「後」。我需要找出如何將「評論」和「添加新評論」模板放入「發佈」模板中的他們的div中。或任何其他方法,使這種結構:骨幹:模板動態視圖層次結構
- post - comments - add new post form - post ...
這是一個類似Facebook的牆
的JavaScript骨幹:
// Post View
var PostView = Backbone.View.extend({
template: $("#post").html(),
...
render: function() {
var tmpl = _.template(this.template);
var thisPost = this.model.toJSON();
this.$el.html(tmpl(thisPost));
}
});
var postView = new PostView();
postView.render();
// Comments List
var CommentsListView = Backbone.View.extend({
el: '#comments', // how to place it to #comments div in "post" template? This line doesn't work
...
addNewCommentForm: function (post_id) {
var tmpl = _.template($("#addCommentTemplate").html());
this.$('#addNewComment').append(tmpl()); // How to place it to #addNewComment div in "post" template? This line doesn't work
}
});
HTML:
<script id="post" type="text/template">
<%= text %>
<div id='comments'>...</div>
<div id='addNewComment'>...</div>
</script>