gpt4 book ai didi

javascript - 遍历集合中的数组并在 Backbone.js 中显示

转载 作者:行者123 更新时间:2023-11-30 08:58:22 25 4
gpt4 key购买 nike

我有一个带有评论列表的待办事项模板。每个待办事项在本地存储中都有一个评论数组“评论”。我获取所有待办事项,遍历每个待办事项的“评论”数组,我希望将所有评论分配给相应的待办事项。如何附加到正确的评论列表?

目前我得到这样的输出:

Post1
Comment1_Post1
Comment2_Post1
Comment1_Post2
Comment2_Post2
Post2
Comment1_Post2
Comment2_Post2

Edit1:新的 CommentCollectionView

 render: function() {  
this.$el.html(this.template());
var commentList = this.$("ul.comment-list");
this.collection.each(function(comment) {
var commentView = new CommentView({model: comment});
commentList.append(commentView.render().el);
});
return this;
},

HTML:

<body>
<div class="content">
<ul class="todos-list"></ul>
</div>

// Templates
<script type="text/template" id="todo-template">
<label class="todo-content"><%= content %></label>
<ul class="comment-list" style="margin-left: 2em"></ul>
</script>

<script type="text/template" id="comment-template">
<label class="comment-content"><%= content %></label>
</script>

待办事项 View :

var TodoView = Backbone.View.extend({
tagName: "li",
template: _.template($("#todo-template").html()),
events: {
"click button.addComment": "addComment"
},
initialize: function() {
_.bindAll(this, "render");
this.model.bind("change", this.render);
var commentsArray = this.model.get("comments");
var commentCollection = new CommentCollection();
commentCollection.add(commentsArray);
var commentCollectionView = new CommentCollectionView({model: commentCollection});
}
});

评论 Collection View :

 var CommentCollectionView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, "render", "appendItem", "addAll", "renderComment");
this.model.bind("reset", this.addAll);
this.model.bind("change", this.render);
this.model.bind("add", this.appendItem);

this.model.trigger("reset");
},
addAll: function() {
this.model.each(this.appendItem);
},
appendItem: function(comment) {
var commentView = new CommentView({model: comment});
$("ul.comment-list").append(commentView.render().el);
}
});

最佳答案

我认为你的问题就在这里:

appendItem: function(comment) {                    
var commentView = new CommentView({model: comment});
$("ul.comment-list").append(commentView.render().el);
}

你的 $('ul.comment-list')找到所有 <ul class="comment-list">当您只想要特定的 <ul> 时对于那个待办事项。

你应该打破你的#todo-template分为两部分:

<script type="text/template" id="todo-template">    
<label class="todo-content"><%= content %></label>
</script>
<script type="text/template" id="comments-template">
<ul class="comment-list" style="margin-left: 2em"></ul>
</script>

然后使用#comments-template作为 CommentCollectionView 的模板:

var CommentCollectionView = Backbone.View.extend({
template: _.template($("#comments-template").html()),
render: function() {
this.$el.html(this.template());
return this;
},
//...
});
var TodoView = Backbone.View.extend({
//...
render: function() {
this.$el.html(this.template(...));

// Use 'collection' here, not 'model'
var c = new CommentCollectionView({collection: commentCollection});
this.$el.append(c.render().el);
return this;
}
});

然后是你的appendItem变成:

appendItem: function(comment) {                    
var commentView = new CommentView({model: comment});
this.$el.append(commentView.render().el);
}

那应该放CommentView入右<ul> .

关于javascript - 遍历集合中的数组并在 Backbone.js 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11409048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com