我有一个已经呈现帖子集合的视图:
Social.Views.StreamsIndex = Backbone.View.extend({
template: JST['streams/index'],
render: function(){
$(this.el).html(this.template({
entries: this.collection.toJSON()
}));
return this;
}
});
现在我必须对一个帖子发表评论,我必须为其渲染一个不同的模板来发表评论:
Social.Views.StreamsIndex = Backbone.View.extend({
template: JST['streams/index'],
events: {
'submit .comment_submit': 'comment_create'
},
comment_create: function(event) {
//create comment code
创建后我想做这样的事情,以便它可以呈现评论模板
$("#comment).html(this.template1({
comment: comment
}));
}
});
是否可以从同一视图呈现两个模板?
编辑:(添加视图)
Social.Views.StreamsIndex = Backbone.View.extend({
template: JST['streams/index'],
template1: JST['streams/comment'],
events: {
'submit .comment_submit': 'comment_create'
},
initialize: function(){
this.collection.on('reset', this.render, this);
this.model = new Social.Models.StreamsIndex();
this.model.bind('comment_createSuccess', this.comment_createSuccess);
},
render: function(){
$(this.el).html(this.template({
entries: this.collection.toJSON()
}));
return this;
},
comment_create: function(event) {
event.preventDefault();
event.stopPropagation();
post_id = $(event.currentTarget).attr("data-post-id");
href = $(event.currentTarget).attr('action');
comment_text = $("#comment_txt_"+post_id).val();
this.model.create_comment(href, post_id, comment_text); // this sends ajax request and post the comment to server
},
comment_createSuccess: function(data, post_id) {
this.$("#comment_for_post_"+post_id).append(this.template1({
comment: data
}));
}
});