I am building a backbone application at the moment, and I have run into a bit of a problem when building a view, the view is built by loop over a collection and outputting HTML. Now the I can output the HTML fine, and see it in my browser, however it always seems to overwrite the previous data, so for example, I am looping over my collection and spitting out the name from the model I should get a list that looks like this,
- File Number 1
- File Number 2
- File Number 3
however the output I am getting is,
- File Number 3
- File Number 3
- File Number 3
So basically on each loop it overwrites itself. How can I stop this happening, I assume it a specifity problem when I am rendering the template?
Below is my view code,
app.FileListItem = Backbone.View.extend({
tagName: 'li',
className: 'image-thumb',
template: _.template($('#tpl-files-image-panel').html()),
events: {
},
initialize: function() {
},
render: function() {
this.model.set({"timeago" : $.timeago(this.model.get('last_modified'))});
//console.log(this.model);
this.$el.html(this.template(this.model.toJSON()));
return this;
},
});
and it gets called like this,
item.get('files').each(function(file){
var itemFileListItem = new app.FileListItem({
model: file
});
that.$el.find('.tab-content.files:first').find('.image-grid').append(itemFileListItem.render().el);
});
item.get('files')
looks like this,
and that.$el
is article.item
of which there are multiple on the page created by another loop.
Can anyone suggest a solution to stop the data getting overwritten on each loop?