我正在尝试使用Pagination plugin for backbone.js来创建无限滚动功能,就像在Twitter中一样,点击按钮/链接#pagination a
将从后端加载下一页结果将其附加到当前视图PhotoListView
。
问题:我正在尝试关注插件的示例here和来源here。到目前为止,我设法通过点击链接data
从后端获取JSON数据#pagination a
。
如何在视图{{1}中的按钮photoListItemView
时,使用backbone.js的模型视图绑定将新视图photoListView
附加到视图#pagination a
点击了吗?
我认为当调用PaginationView
中this.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;
}
});
答案 0 :(得分:0)
正如基肖尔所说的那样。绑定到reset
事件,现在可以使用。