2013-03-28 30 views
7

Backbone.js cung cấp xác thực cho các mô hình. Nhưng không có cách nào đơn giản để kiểm tra tất cả các mô hình trong bộ sưu tập là hợp lệ. Không có tài sản .isValid cho các bộ sưu tập.Backbone.js xác thực bộ sưu tập

tôi sử dụng một hack như thế này:

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

Có cách tối ưu hơn vào bộ sưu tập 'Validate'?

+0

Dường như chỉ lặp qua bộ sưu tập và kiểm tra .isValid sẽ làm những gì bạn đang yêu cầu. Sử dụng. Để làm việc lặp lại ... – kinakuta

Trả lời

8

Còn cách sử dụng phương pháp some thì sao?

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

Cảm ơn rất nhiều! Nó rõ ràng là phù hợp hơn. – JuliaCesar

1

lodash.js hỗ trợ xây dựng như thế này (với "_.pluck" callback viết tắt):

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

Tôi biết rằng đây là một câu hỏi cũ nhưng hãy nhìn vào quyết định của tôi cho vấn đề này. Trong ngắn hạn, nó có sẵn như github repo backbone-collection-validation. Bây giờ, để biết chi tiết. Để xác nhận bộ sưu tập như thế này

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

Bạn cần phải mở rộng Backbone của bạn với điều này

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

hoặc với điều này

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

Các BackboneJS Collection delegates một số phương pháp UnderscoreJS như some. Bạn có thể sử dụng nó như thế:

var hasErrors = myCollection.some(function(model) { 
    return model.validationError; 
}); 
Các vấn đề liên quan