Last active
August 29, 2015 14:13
-
-
Save nfvs/a58cdcd56279748efab1 to your computer and use it in GitHub Desktop.
Custom backbone.js Collection.fetch() that doesn't execute if the last instance hasn't completed yet.
This file contains 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
// Override the default Backbone.collection.fetch() | |
// to avoid being called before the last one has finished | |
Backbone.Collection.prototype.fetch = _.wrap( | |
Backbone.Collection.prototype.fetch, | |
function(original_fetch) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
// Check if the last fetch() is still underway, if so skip this one. | |
if (this.fetch_pending === true) return; | |
this.fetch_pending = true; | |
try { | |
return original_fetch.apply(this, args).always(function() { self.fetch_pending = false; }); | |
} catch(err) { | |
this.fetch_pending = false; | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment