Skip to content

Instantly share code, notes, and snippets.

@webmat
Created May 4, 2011 15:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save webmat/955419 to your computer and use it in GitHub Desktop.
Save webmat/955419 to your computer and use it in GitHub Desktop.
Getting pagination information from headers with Backbone
// Get the pagination information from the response headers
// Wrap Backbone.sync to save the pagination header on the response object.
Backbone.sync_with_pagination = function(method, model, success, error) {
var success_with_pagination = function(resp, status, xhr) {
resp.next_page = xhr.getResponseHeader('X-Next-Page');
return success(resp);
}
return Backbone.sync_without_pagination(method, model, success_with_pagination, error);
};
Backbone.sync_without_pagination = Backbone.sync;
Backbone.sync = Backbone.sync_with_pagination;
// Wrap Collection.refresh to transfer the pagination info from the response object to the collection.
Backbone.Collection.prototype.refresh_with_pagination = function(models, options) {
this.next_page = models.next_page;
return this.refresh_without_pagination(models, options);
};
Backbone.Collection.prototype.refresh_without_pagination = Backbone.Collection.prototype.refresh;
Backbone.Collection.prototype.refresh = Backbone.Collection.prototype.refresh_with_pagination;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment