JavaScript Journal - looping
Depending on when you started learning JavaScript (and when you stopped keeping up) you might find a lot of this inside your code
for(var i=0;i<arr.length;i++) {
html = "<li>" + arr[i] + "</li>"
}
or if you started with jQuery you will have copy and pasted something like this from stackoverflow
$.getJSON(url, function(data) {
$.each(data, function(item) {
$('#thing').append('<li>' + item + '</li>')
}
}
or you could be doing something like this and use a client-side templete, Each model would get its own view
LayoutView = Backbone.View.extend({
events: {},
tagName: 'div',
className: 'een-camera-holder',
template: _.template($('#LayoutView-template').html()),
initialize: function() {
this.model.bind('change', this.render, this)
},
render: function() {
this.$el.html(this.template(this.model.toJSON()))
return this
}
})
I'm not sure what will come next in ES6 but I'm sure this will need to be updated in 6 months with let
and yield