2013-03-28 98 views
7

Backbone.js爲模型提供驗證。但是沒有一種簡單的方法來檢查收集中的所有模型是否有效。否.isValid收藏屬性。Backbone.js驗證集合

我用這樣一個黑客:

_.isEmpty(_.filter(myCollection.models, function(m) {return m.validationError;})) 

是否有「驗證」收集更優化的方式?

+0

似乎只是迭代你的收集和檢查.isValid會做你在問什麼。使用.each來做迭代... – kinakuta 2013-03-28 07:17:53

回答

8

如何使用some方法?

var hasErrors = _.some(myCollection.models, function(m) { 
    return m.validationError; 
}); 
+0

非常感謝!這顯然更合適。 – JuliaCesar 2013-03-28 07:51:13

1

lodash.js支持建設這樣的(用「_.pluck」回調簡寫):

_.some(myCollection.models, 'validationError'); 
0

我知道這是一個老問題,但請看看我決定這個問題。總之,它可以作爲github回購backbone-collection-validation。現在,細節。爲了驗證收集這樣

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.'; 
    } 
    } 
}) 

您需要與此

//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; 
}; 

擴展您的骨幹或與此

//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); 
    } 
    } 
}; 
3

的BackboneJS收藏delegates一些UnderscoreJS方法,如some。你可以這樣使用它:

var hasErrors = myCollection.some(function(model) { 
    return model.validationError; 
});