我会在视图本身上执行此操作,而不是覆盖sync
或fetch
本身。
就像是:
// when extending your view
initialize: function(options) {
//...
this.collection.on('add', this.renderTenant, this);
},
events: {
// change the selector to match your "more" button
'click button.more': 'uiMore'
},
// Just tacking this on the view. You could make it an option, or whatever.
perPage: 50,
// this would produce a query with `offset` and `length`. Change it to
// however your request should paginate: page/perPage, just page, etc.
uiMore: function() {
var $more = this.$('.more');
var data = {};
data.offset = this.collection.length;
data.length = this.perPage;
$more.prop('disabled', true);
this.collection.fetch({data: data, add: true, success: function() {
$more.prop('disabled', false);
});
},
renderTenant: function(tenant) {
var view = new TenantView({
model: tenant,
collection: this.collection
})
this.$el.append(view.render().el);
},
render: function(){
this.collection.each(this.renderTenant.bind(this));
return this;
}