admin管理员组文章数量:1356709
I have a server that works with a header ETag. Backbone refers to the API for the first time: everything is good, the response received and parse. Second time: backbone sends to the server ETag, in response receives NotModified. And Backbone trying to parse this response, resulting in a collection called reset.
Is there any way around it resets the collection?
The method of adding the option to add in the fetch method will not work. Since I need to pletely refresh the collection, if I came to the server's response.
var remendCollection = Backbone.Collection.extend({
model : Event,
etag : null,
urlRoot : '/api/users',
initialize: function() {
this.etag = null;
},
parse: function(response) {
return response.data;
},
url : function () {
return (this.urlRoot + "/"+window.me.get('id')+ "/remendation");
},
beforeSend : function (jqXHR, settings) {
jqXHR.setRequestHeader('if-none-match', this.etag);
},
plete : function (jqXHR, textStatus) {
if (jqXHR.status == 200 || jqXHR.status == 304) {
this.etag = jqXHR.getResponseHeader('ETag');
}
},
update : function () {
this.fetch({
beforeSend : this.beforeSend.bind(this),
plete : thisplete.bind(this),
data : {
cityId : window.me.get('cityId'),
}
});
}
I have a server that works with a header ETag. Backbone refers to the API for the first time: everything is good, the response received and parse. Second time: backbone sends to the server ETag, in response receives NotModified. And Backbone trying to parse this response, resulting in a collection called reset.
Is there any way around it resets the collection?
The method of adding the option to add in the fetch method will not work. Since I need to pletely refresh the collection, if I came to the server's response.
var remendCollection = Backbone.Collection.extend({
model : Event,
etag : null,
urlRoot : '/api/users',
initialize: function() {
this.etag = null;
},
parse: function(response) {
return response.data;
},
url : function () {
return (this.urlRoot + "/"+window.me.get('id')+ "/remendation");
},
beforeSend : function (jqXHR, settings) {
jqXHR.setRequestHeader('if-none-match', this.etag);
},
plete : function (jqXHR, textStatus) {
if (jqXHR.status == 200 || jqXHR.status == 304) {
this.etag = jqXHR.getResponseHeader('ETag');
}
},
update : function () {
this.fetch({
beforeSend : this.beforeSend.bind(this),
plete : this.plete.bind(this),
data : {
cityId : window.me.get('cityId'),
}
});
}
Share
Improve this question
edited Jun 20, 2012 at 7:04
Anthony Tsivarev
asked Jun 20, 2012 at 6:41
Anthony TsivarevAnthony Tsivarev
8913 gold badges14 silver badges25 bronze badges
2 Answers
Reset to default 8As far as I can tell, there is no easy solution to trap a 304 response. What I came up with:
parse
receives a second argument,options
, that you can use to check the status of the request and repopulate your collection with the same models if necessary. It works, but it will trigger areset
parse: function(response, options) { if (options.xhr.status === 304) return this.models return response.data; }
http://jsfiddle/nikoshr/sxv9P/12/
jQuery accepts an
ifModified
option that may help youifModified Default: false
Allow the request to be successful only if the response has changed since the last request. This is done by checking the Last-Modified header. Default value is false, ignoring the header. In jQuery 1.4 this technique also checks the 'etag' specified by the server to catch unmodified data.
Or override the
fetch
function to stop on a 304 responsefetch: function(options) { options = options ? _.clone(options) : {}; if (options.parse === undefined) options.parse = true; var collection = this; var success = options.success; options.success = function(resp, status, xhr) { if (xhr.status!==304) collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options); if (success) success(collection, resp); }; options.error = Backbone.wrapError(options.error, collection, options); return (this.sync || Backbone.sync).call(this, 'read', this, options); }
http://jsfiddle/sxv9P/1/
The 304 response MUST NOT contain a message-body. Sorry, was too hurry answering, my words don't help here.
本文标签: javascriptBackbonejs parse notmodified responseStack Overflow
版权声明:本文标题:javascript - Backbone.js parse not-modified response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744004391a2574433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论