【发布时间】:2015-01-31 09:23:27
【问题描述】:
我正在尝试使用 Backbone.View 为集合呈现视图。但我无法渲染评论视图以将单个 cmets 显示为 cmets 视图内的列表。仅呈现 cmets 形式。当从地址栏访问集合的 url 时,返回下面的数组,因为我用 express 在服务器端代码中编写了它。
我无法解决的问题可能是什么?用这段代码实现它似乎很自然,但可以肯定的是我遗漏了一些东西。一般问题是我被困在如此详细的点上,尽管我可以学习说 mvc 框架、Backbone、Node、express 等。
评论视图:
var CommentsView = Backbone.View.extend({
initialize: function (options) {
this.post = options.post;
this.collection = new Comments({post: this.post});//injecting collection from this views options, was injected to this view form router.
this.collection.on('add', this.addComments, this);
},
addComments: function (comment) {
this.$el.append(new CommentView({ model: comment }).render().el);
},
render: function () {
this.$el.append("<h2> Comments </h2>");
this.$el.append(new CommentFormView({post: this.post}).render().el);
return this;
}
});
这是从地址栏访问集合的url时返回的数组:
[
{
"_id": "547e36df0ca19b2c1a6af910",
"postId": "547d00e30ca19b2c1a6af90b",
"name": "comment one",
"date": "Mon 21 Oct 2014",
"text": "gibberjabber"
}
]
当相关帖子的cmets的路由被路由到时的Router方法:
comments: function(_id) {
var csv = new CommentsView({ post: this.posts.findWhere( {_id: _id} ) });
this.main.html(csv.render().el);
}
【问题讨论】:
-
显示
CommentView的代码,你确定addComments被执行了吗?你能创建一个小提琴来演示这个问题吗?