Looking to optimize populating a Backbone collection, I have come across the push method but it requires iterating over all items:
define([
...
], function($, _, Backbone, imagesCollection, imageTemplate, gridView) {
var AppView = Backbone.View.extend({
el: '#container',
template: _.template( imageTemplate ),
events: {
'click #search': 'search'
},
initialize: function() {
this.input = this.$('#search-term');
},
populate: function(data) {
for (var i=0;i<data.length;i++) {
imagesCollection.push(data[i]);
}
//IS THERE ANY WAY TO PREVENT ITERATING OVER ALL THE ITEMS?
},
search: function() {
$.ajax({
type: 'get',
url: myurl,
dataType:'jsonp',
success: function(response){
populate(response);
}
});
}
});
return AppView;
});
Seeking alternatives to iterate efficiently through items in a Backbone collection. Your suggestions and feedback are appreciated as I am relatively new to backbone development.