Skip to content

Instantly share code, notes, and snippets.

@koozdra
Last active October 16, 2015 22:39
Show Gist options
  • Save koozdra/657200f526729e5351c9 to your computer and use it in GitHub Desktop.
Save koozdra/657200f526729e5351c9 to your computer and use it in GitHub Desktop.
Functionalizing
Practicing underscore foo...
checkErrors: function(attrs) {
var errors = [];
_.each(this.basicValidationFields, function(key) {
errors.push(this.validatePresence(key, attrs[key]));
}.bind(this));
errors = _.compact(errors);
if (errors.length) return errors;
},
validatePresence: function(key, value) {
if (!value) {
return { name: key, error: this.app.i18n.t('errors.comments.empty_comment')};
}
}
----------------------------------------------------
checkErrors: function(attrs) {
function toErrors(attrs, t, key) {
if (attrs[key]) {
return {
name: key,
error: t('errors.comments.empty_comment')
};
}
}
var errors = _(this.basicValidationFields)
.map(toErrors.partial(attrs, this.app.i18n.t).bind(this))
.compact()
.value();
if (errors.length) return errors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment