我正在尝试使用 Pagination plugin让 backbone.js 创建无限滚动功能,就像在 Twitter 中一样,单击按钮/链接 #pagination a
将从后端加载下一页结果并将其附加到当前 View 照片 ListView
。
问题:我正在尝试遵循插件的示例 here和来源here .到目前为止,我设法通过单击链接 #pagination a
从后端获取 JSON 数据 data
。
当按钮 #pagination a
View 中的 PaginationView
是否被点击?
我以为paginationView
中的appendRender
方法的this.collection.requestNextPage()
被调用时,会添加新获取的模型到集合 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();
最佳答案
正如 Kishore 所说。绑定(bind)到 reset
事件,现在可以使用了。
关于javascript - backbone.js 上的无限滚动/分页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11353454/