Reputation: 1029
Backbone.js offers validation for models. But there is no a simple way to check all models in collection are valid. No .isValid
property for collections.
I use a hack like this:
_.isEmpty(_.filter(myCollection.models, function(m) {return m.validationError;}))
Is there more optimized way to 'validate' collection?
Upvotes: 7
Views: 4563
Reputation: 8695
The BackboneJS Collection delegates some UnderscoreJS methods like some
. You could use it like that:
var hasErrors = myCollection.some(function(model) {
return model.validationError;
});
Upvotes: 3
Reputation: 4829
I know that this is an old question but please look at my decision to this problem. In short, it is available as github repo backbone-collection-validation. Now, to details. To validate collection like this
Collection = Backbone.Collection.extend({
validate: function (collection) {
var nonExistIds = [];
_.forEach(collection, function (model) {
var friends = model.get('friends');
if (friends && friends.length) {
for (var i = friends.length - 1; i >= 0; i--) {
if (!this.get(friends[i])) {
nonExistIds.push(friends[i]);
}
}
}
}, this);
if (nonExistIds.length) {
return 'Persons with id: ' + nonExistIds + ' don\'t exist in the collection.';
}
}
})
You need to extend your Backbone with this
//This implementation is called simple because it
// * allows to set invalid models into collection. Validation only will trigger
// an event 'invalid' and nothing more.
var parentSet = Backbone.Collection.prototype.set;
Backbone.Collection.prototype.set = function (models, options) {
var parentResult = parentSet.apply(this, arguments);
if (options && options.validate) {
if (!_.isFunction(this.validate)) {
throw new Error('Cannot validate a collection without the `validate` method');
}
var errors = this.validate(this.models);
if (errors) {
this.trigger('invalid', this, errors);
}
}
return parentResult;
};
or with this
//This implementation is called advanced because it
// * doesn't allow to set invalid models into collection.
var parentSet = Backbone.Collection.prototype.set;
Backbone.Collection.prototype.set = function (models, options) {
if (!options || !options.validate) {
return parentSet.apply(this, arguments);
} else {
if (!_.isFunction(this.validate)) {
throw new Error('Cannot validate a collection without the `validate` method');
}
var clones = [];
_.forEach(this.models, function (model) {
clones.push(model.clone());
}, this);
var exModels = this.models;
this.reset(clones);
var exSilent = options.silent;
options.silent = true;
parentSet.apply(this, arguments);
var errors = this.validate(this.models);
this.reset(exModels);
if (typeof exSilent === 'undefined') {
delete options.silent;
} else {
options.silent = exSilent;
}
if (errors) {
this.trigger('invalid', this, errors);
return this;
} else {
return parentSet.apply(this, arguments);
}
}
};
Upvotes: 0
Reputation: 1029
lodash.js supports construction like this (with "_.pluck" callback shorthand):
_.some(myCollection.models, 'validationError');
Upvotes: 1