У меня есть представление, которое уже отображает коллекцию сообщений:
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
}));
}
});