2013-03-28 17 views
7

Backbone.js oferuje sprawdzanie poprawności dla modeli. Ale nie ma prostego sposobu sprawdzenia, czy wszystkie modele w kolekcji są prawidłowe. No .isValid właściwość kolekcji.Potwierdzenie kolekcji Backbone.js

używam hack takiego:

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

tam jest bardziej zoptymalizowany sposób, aby 'validate' kolekcji?

+0

Wygląda na to, że po prostu przechodzę przez Twoją kolekcję i sprawdzam. ToValid zrobi to, o co prosisz. Użyj .each do iteracji ... – kinakuta

Odpowiedz

8

Co z wykorzystaniem metody some?

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

Wielkie dzięki! Jest to oczywiście bardziej odpowiednie. – JuliaCesar

1

lodash.js wspiera budowę tak (z „_.pluck” callback skrót):

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

wiem, że jest to stare pytanie, ale proszę spojrzeć na moją decyzję tego problemu. W skrócie, jest dostępny jako github repo backbone-collection-validation. Teraz, do szczegółów. Aby potwierdzić kolekcję jak ten

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

musisz przedłużyć szkieletowej z tym

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

lub niniejszego

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

backbone.js Collection delegates niektóre underscore.js podobne metody some. Możesz go użyć w ten sposób:

var hasErrors = myCollection.some(function(model) { 
    return model.validationError; 
}); 
Powiązane problemy