我有三个不同的视图模板:“发布”、“评论”、“添加新评论”。主要模板是“post”。我需要了解如何将“评论”和“添加新评论”模板放入“帖子”模板中的 div 中。或任何其他方法来制作这个结构:
- 邮政 - 评论 - 添加新的帖子表单 - 邮政 ...
它类似于脸书墙
用于骨干网的 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>