alekseykulikov / backbone-offline

[Deprecated] Allows your Backbone.js app to work offline
MIT License
720 stars 56 forks source link

Error handler in collection.fetch() is never called #27

Open asgeo1 opened 11 years ago

asgeo1 commented 11 years ago

In Backbone.offline, if I make a call to refresh() to update my collection from the server, the error handler will not get called. (assuming an error did occur and it needed to be called)

collection.fetch({
    success: function(){
        console.log('success');
    },
    error: function(){
        console.log('error');
    }
});

Looking at the code, it's because the options object passed into fetch() is not passed along the chain. There a few functions like this in the code:

Sync.prototype.pull = function(options) {
  var _this = this;
  if (options == null) options = {};
  return this.ajax('read', this.collection.items, {
    success: function(response, status, xhr) {
      var item, _i, _len;
      _this.collection.destroyDiff(response);
      for (_i = 0, _len = response.length; _i < _len; _i++) {
        item = response[_i];
        _this.pullItem(item);
      }
      if (options.success) return options.success();
    }
  });
};

where the options parameter is not passed into the ajax calls. Only a success handler is passed. Which means my error handler will never make it into the call to Backbone.ajaxSync | Backbone.sync, where it needs to be.