backbone-generator/templates/app_helper.js.erb

30 lines
766 B
Plaintext

/* Ensure a Collection is fetched and has entries before running a block of code. */
_.extend(Backbone.Collection.prototype, {
ensureFetched: function(callback) {
if (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);
}
}
});
_.extend(Backbone.View.prototype, {
attributes: function() {
var attrs = {};
var _this = this;
_.each(this.attributeFields, function(field) {
attrs[field] = _this.$('input[name="' + field + '"]').val();
});
return attrs;
}
});