-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
parse() no longer passes XHR object #1939
Comments
The removal of the {
"page": 5,
"ofPages": 25,
"models": [{},{},{}]
} And then parse: function (data) {
// use data.page and data.ofPages as needed
return data.models;
} |
You can make your response to carry this meta-data. For example a bulk response can look something like this: {
meta: {
next: '/api/next/resources/list',
prev: '/api/prev/resources/list',
limit: 5,
offset: 5,
count: 5
},
objects: [{id: 6}, {id: 7}, {id: 8}, {id: 9}, {id: 10}]
} |
Nah, @caseywebdev beat me up to it :) |
I've been thinking about this issue as well. There are many public APIs that support pagination by way of sending down headers, e.g. Github. Those aren't APIs that users of Backbone have control with so changing the server API is out of the question. Removing |
Working on a solution...stay tuned. |
I understand the desire for For others trying to deal with this change in 0.9.9 with an API they don't control, the workaround is less elegant but fairly simple by overriding the collection's sync: function() {
var _this, xhr;
_this = this;
xhr = Backbone.Collection.prototype.sync.apply(this, arguments);
// pass xhr to first reset event:
this.once('reset', function() {
_this.onReset(xhr);
});
return xhr;
},
onReset: function(xhr) {
// stash some arbitrary headers here:
this.count = xhr.getResponseHeader('Count')
} |
Hmmm. Removing sync: function (method, model, options) {
var success = options.success;
options.success = function (reps, status, xhr) {
// do something with xhr before parsing
if (success) success(resp, status, xhr);
};
return Backbone.Collection.prototype.sync.call(this, method, model, options);
} the other is to use col.fetch().done(function (resp, status, xhr) {
// do something with `xhr`
}); Unfortunately, this method only works with jQuery at the moment as Zepto does not yet return a If |
|
Fix #1939 - `parse` receives `options`
First off, congrats and thanks for the bounty of great changes in 0.9.9.
One issue I'm encountering with a project previously based on 0.9.2 is that
parse
no longer passes the XHR object with the models.The use case here is that the client side is fetching a paginated collection and the remote API returns the total length of the collection in the XHR's headers. The app can then display the total count to the user and lazily fetch extra pages.
Is there a better way to implement this behavior from the client side without relying on the XHR passed to
parse()
?I wanted to clarify before reverting the behavior in Backbone.
The text was updated successfully, but these errors were encountered: