Skip to content

Instantly share code, notes, and snippets.

@nfvs
Last active August 29, 2015 14:13
Show Gist options
  • Save nfvs/a58cdcd56279748efab1 to your computer and use it in GitHub Desktop.
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.
// 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