我正在尝试使用 Backbone.js 的分页插件来创建一个无限滚动功能,就像在 Twitter 中一样,单击按钮/链接#pagination a
将从后端加载下一页结果并将其附加到当前视图PhotoListView
。
问题:我正在尝试在此处遵循插件的示例并在此处获取源代码。到目前为止,我设法通过单击链接从后端获取 JSON 数据。data
#pagination a
当单击视图中的按钮时,如何利用backbone.js 的模型视图绑定将新视图附加photoListItemView
到视图中?photoListView
#pagination a
PaginationView
我认为当调用inthis.collection.requestNextPage()
方法时,新检索到的模型将被添加到将触发事件触发器的集合中,从而导致新检索到的模型被追加?appendRender
paginationView
photoCollection
photoListView
this.collection.bind('change', this.render, this);
意见
PhotoListView = Backbone.View.extend({
el: '#photo_list',
initialize: function() {
this.collection.bind('change', this.render, this);
this.collection.bind('add', function() {
console.log('added to collection');
}, this);
},
render: function() {
//$(this.el).html('');
this.collection.each(function(photo, index) {
if(index % 7 < 3) {
$(this.el).append(new PhotoListItemView({ model: photo }).render().el);
} else {
$(this.el).append(new PhotoListQuadItemView({ model: photo }).render().el);
}
console.log('render');
}, this);
return this;
}
});
PhotoListItemView = Backbone.View.extend({
tagNAme: 'div',
className: 'photo_box',
template: _.template( $('#tpl_PhotoListItemView').html() ),
initialize: function() {
this.model.bind('destroy', this.close, this);
},
render: function() {
$(this.el).html( this.template( this.model.toJSON() ) );
console.log('render item');
return this;
},
close: function() {
this.unbind();
this.remove();
}
});
PhotoListQuadItemView = Backbone.View.extend({
tagNAme: 'div',
className: 'photo_box',
template: _.template( $('#tpl_PhotoListQuadItemView').html() ),
initialize: function() {
this.model.bind('destroy', this.close, this);
},
render: function() {
$(this.el).html( this.template( this.model.toJSON() ) );
return this;
},
close: function() {
this.unbind();
this.remove();
}
});
PaginationView = Backbone.View.extend({
el: $('#pagination'),
events: {
'click #pagination a': 'appendRender'
},
initialize: function() {
this.render();
},
template: _.template( $('#tpl_pagination').html() ),
render: function() {
$(this.el).html( this.template() );
},
appendRender: function() {
this.collection.requestNextPage()
.done(function(data, textStatus, jqXHR) {
// HOW DO I APPEND THE NEW DATA VIEWS?
});
}
});
收藏
PhotoCollection = Backbone.Paginator.requestPager.extend({
model: Photo,
paginator_core: {
dataType: 'json',
url: 'http://www.domain.com/explore/all?'
},
paginator_ui: {
firstPage: 1,
currentPage: 1,
perPage: 7,
totalPages: 10
},
server_api: {
'$page': function() { return this.currentPage; }
},
parse: function (response) {
return response;
}
});
路由器
var AppRouter = Backbone.Router.extend({
routes: {
'': 'explore'
},
initialize: function() {
},
explore: function() {
console.log('explore!');
this.photoList = new PhotoCollection();
this.paginationView = new PaginationView({ collection: this.photoList });
var self = this;
this.photoList.fetch({
success: function() {
self.photoListView = new PhotoListView({ collection: self.photoList });
self.photoListView.render();
}
});
}
});
var app = new AppRouter();
Backbone.history.start();