理论上,您需要fetch
从某处异步获取一些内容来显示加载器。需要加载以向用户显示您实际上正在获取内容并且 UI 没有死。在那个小提琴中,即使你让它工作了,你也看不到它,因为集合是自举的,你没有获取任何东西。
这模拟了(更新了你的小提琴):
app.BookListView = Backbone.View.extend({
el: '.feed',
initialize: function() {
this.loader();
this.listenToOnce( this.collection, 'sync', this.render); // or listenTo ?
this.listenTo( this.collection, 'add', this.renderBook );
// this simulates the fetching...
// It's not really needed
var self = this;
setTimeout(function(){
self.collection.trigger('sync');
}, 3000)
},
loader: function(){
this.$el.html('<div>Loading...</div>')
},
render: function() {
this.$el.empty();
this.collection.each(function( item ){
this.renderBook( item );
}, this);
},
renderBook: function ( item ) {
var bookview = new app.BookView ({
model: item
});
this.$el.append( bookview.render().el );
}
});