Created
May 4, 2011 15:30
-
-
Save webmat/955419 to your computer and use it in GitHub Desktop.
Getting pagination information from headers with Backbone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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