backbone-generator/templates/app_helper.js.erb

20 lines
529 B
Plaintext
Raw Normal View History

2011-04-23 15:13:54 +00:00
/* Ensure a Collection is fetched and has entries before running a block of code. */
_.extend(Backbone.Collection.prototype, {
ensureFetched: function(callback) {
if (this.length == 0 || this._alreadyEnsureFetched) {
var _this = this;
var _refresher = function() {
_this.unbind('refresh', _refresher);
callback.apply(_this);
_this._alreadyEnsureFetched = true;
};
this.bind('refresh', _refresher);
this.fetch();
} else {
callback.apply(this);
}
}
});