2013-03-28 5 views
7

Backbone.js은 모델 검증을 제공합니다. 그러나 컬렉션의 모든 모델을 검사하는 간단한 방법은 없습니다. 아니 .isValid 컬렉션에 대한 속성. '검증'컬렉션에 더 최적화 된 방법은Backbone.js 유효성 검사 컬렉션

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

있습니까 :

는이 같은 해킹을 사용 하는가?

+0

컬렉션을 반복하면서 .isValid를 확인하면 원하는 것을 할 수 있습니다. .each를 사용하여 반복 작업을 수행하십시오 ... – kinakuta

답변

8

some 방법은 어떻게 사용하나요?

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

고마워요! 그것은 분명 더 적합합니다. – JuliaCesar

1

lodash.js이 (와 "_.pluck"콜백 속기) 등의 건설을 지원합니다. 즉, github repo backbone-collection-validation으로 사용할 수 있습니다. 자, 자세한 내용을. 이 당신이

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

로 백본을 확장 할 필요가

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

또는 BackboneJS 컬렉션 delegatessome 같은 일부 UnderscoreJS 방법이

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

나는이 오래된 질문이다 것을 알고 있지만이 문제에 대한 나의 결정을보고하십시오

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

와 같은 모음을 확인합니다. 다음과 같이 사용할 수 있습니다 :

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