Skip to content

Instantly share code, notes, and snippets.

@asalant
Created December 18, 2012 01:51
Show Gist options
  • Save asalant/4324285 to your computer and use it in GitHub Desktop.
Save asalant/4324285 to your computer and use it in GitHub Desktop.
Mixin for Backbone Model and Collection classes that will ensure that all ajax calls are run serially instead of concurrently (the default). Works by chaining jQuery Deferred.
var MyModel = Backbone.Model.extend({});
withSerializedSync(MyModel);
withSerializedSync = function(cls) {
var sync = cls.prototype.sync || Backbone.sync;
cls.prototype.sync = function() {
var args = arguments.length ? Array.prototype.slice.call(arguments,0) : [];
if (!this._lastSync) {
this._lastSync = sync.apply(this, args);
} else {
var _this = this;
this._lastSync = this._lastSync.then(function() {
return sync.apply(_this, args);
});
}
return this._lastSync;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment