最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone.js parse not-modified response - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 8

As 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 a reset

    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 you

    ifModified 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 response

    fetch: 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.

发布评论

评论列表(0)

  1. 暂无评论