Created
December 18, 2012 01:51
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MyModel = Backbone.Model.extend({}); | |
withSerializedSync(MyModel); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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