jeromegn/Backbone.localStorage

Allow override sync method using options

w0rm opened this issue · 2 comments

w0rm commented

I have a session model, that I need to sync with both back-ends (api endpoint and localStorage).

It could be good to choose sync method not only based on model.localStorage || (model.collection && model.collection.localStorage) expression, but also based on options object. What do you think of this change?

Backbone.getSyncMethod = function(model, options) {
  if(model.localStorage || (model.collection && model.collection.localStorage)) {
    if(!options || !options.syncMethod || options.syncMethod === 'localStorage') { 
       return Backbone.localSync;
    }
  }

  return Backbone.ajaxSync;
};

// Override 'Backbone.sync' to default to localSync,
// the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
Backbone.sync = function(method, model, options) {
  return Backbone.getSyncMethod(model, options).apply(this, [method, model, options]);
};

#166 implemented the option {ajaxSync: true}

Yes, seems old and fixed.