Skip to content

Instantly share code, notes, and snippets.

@abstraktor
Created September 27, 2011 15:52
Show Gist options
  • Save abstraktor/1245449 to your computer and use it in GitHub Desktop.
Save abstraktor/1245449 to your computer and use it in GitHub Desktop.
adding a loaded flag to Backbone Collections via extension
/*
*= require backbone-rails
*/
(function() {
var _collection = Backbone.Collection;
Backbone.Collection = function() {
// I would like to use Backbone.Events on myself
_.extend(this, Backbone.Events);
// this collection is obviously not loaded
this.loaded = false;
// set loaded if I loaded something
var setLoaded = _.once(function() {
this.loaded = true;
});
this.bind("reset", setLoaded, this);
this.bind("add", setLoaded, this);
// now lets do the actual initialisation
_collection.apply(this, arguments);
}
Backbone.Collection.prototype.loaded = false;
_.extend(Backbone.Collection.prototype, _collection.prototype);
_.extend(Backbone.Collection, _collection);
}).apply(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment