javascript - Iterate through an array in a collection and display in Backbone.js -


I have found a template with a list for comment, in the comments of the local workers in every audio "comments" array I get all the Tudos, work again through the "comments" array of each Todo and I want all comments related to this. How do I join the right commentary list?

Currently I get the output like this:

  1 comment 1_Post1 comment 2_Post1 comment1_Post2 comment 2_Post2 post 2 comment 1_Post2 comment2_Post2   

Edit 1: View new comment reference

  Render: function () {this. $ El.html (this.template ()); Var Comment List = It $ ("Ul.comment-list"); This.collection.each (function (comment) {var commentview = new commentview ({model: comment}); comment list .append (commentView.render (). El);}); This return; },   

HTML:

  & lt; Body & gt; & Lt; Div class = "content" & gt; & Lt; Ul class = "todos-list" & gt; & Lt; / Ul & gt; & Lt; / Div & gt; // template & lt; Script type = "text / template" id = "todo-template" & gt; & Lt; Label class = "todo-content" & gt; & Lt;% = content% & gt; & Lt; / Labels & gt; & Lt; Ul class = "comment-list" style = "margin-left: 2em" & gt; & Lt; / Ul & gt; & Lt; / Script & gt; & Lt; Script type = "text / template" id = "comment-template" & gt; & Lt; Label class = "comment-content" & gt; & Lt;% = content% & gt; & Lt; / Labels & gt; & Lt; / Script & gt; TodoView = Backbone.View.extend ({tagName: "li", Template: _.template ($ ("(# $ (" # "#")   

Todo-template ") Html ()), Event: {"click button.addcomment": "addcomment"}, start: function () {_.bindAll (this, "render"); this.model.bind ("change", this .render); var commentsArere = this.model.get ("comments"); var CommentCollection = New CommentCompling (); CommentClose.Ad (commentsAir); var CommentConstruction view = new comment context view ({Model: commentcollection});}}) ;

View the comments archive:

  var CommentCollectionView = Backbone.View extension ({start: function () {_.bindAll (this, "render", "append itam", "adl", "reader comment"); 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 comment view ({Model: comment}); $ ("Ul.comment-list") Attachment (commentView.render (.) El);}});  

The problem is in the definition of TodoModel, you have not pasted the code but I I can assume the issue, there is a common problem with data type objects.

There is a feature in TodoMold that you are defining in your default way like this:

  TodoModel = Backbone.Model.extend ({default: {// some comments: []}});   

So the comments are an array that is always being used in all your examples TodoModel to change your default in such a way:

  var todomold = backbone .mode.exend ({default: function () {returns {// some comments: []}}});   

Update, because there is a problem with the scope of the jQuery selector: to make sure that you only use the DOM elements of your current view. Instead of the usual jQuery call, example: $. ('Ul.comment-list')

Comments