Since Collections is actually a model, it has attributes and so on, like in this example
var Images = Backbone.Collection.extend({
url: 'https://api.myjson.com/bins/4v2d8'
});
var View = Backbone.View.extend({
el: $('.images'),
initialize: function(){
this.listenTo(this.collection, 'sync', this.render, this); // what is this keyword as the last param here?
},
render: function(){
this.collection.each(function(model){
this.$el.append($('<p>'+model.get('name')+'</>' ));
}, this);
}
});
$(function(){
var images = new View({ collection: new Images() });
images.collection.fetch();
});
http://jsbin.com/gohedeguto/edit?html,js,output
But why this one doesn't work?
http://jsbin.com/seyoyeqeku/edit?html,js,output
I replaced Collection
with Model
and passed to the View. I got this.model.each of undefined
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…