我有一个骨干集合,我想创建一个填充集合的方法。我发现的是推送方法,但这需要迭代所有项目:填充骨干中的集合
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;
});
是否有任何其他解决方案? 我是新来的骨干,所以如果你看到任何错误 - 请告诉我。
您可以使用接受模型或模型数组的[add](http://documentcloud.github.com/backbone/#Collection-add)方法。 – Jack