Created
September 27, 2011 15:52
-
-
Save abstraktor/1245449 to your computer and use it in GitHub Desktop.
adding a loaded flag to Backbone Collections via extension
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
/* | |
*= 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