7

Backbone.js为模型提供验证。但是没有一种简单的方法可以检查集合中的所有模型是否有效。没有.isValid收藏品。

我使用这样的黑客:

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

是否有更优化的方式来“验证”收藏?

4

4 回答 4

8

使用某种方法怎么样?

var hasErrors = _.some(myCollection.models, function(m) {
    return m.validationError;
});
于 2013-03-28T07:30:52.407 回答
3

BackboneJS Collection委托了一些 UnderscoreJS 方法,例如some. 你可以这样使用它:

var hasErrors = myCollection.some(function(model) {
    return model.validationError;
});
于 2015-03-25T10:19:40.187 回答
1

lodash.js支持这样的构造(使用“_.pluck”回调简写):

_.some(myCollection.models, 'validationError');
于 2013-05-13T08:29:00.410 回答
0

我知道这是一个老问题,但请看看我对这个问题的决定。简而言之,它可以作为 github repo主干集合验证。现在,到细节。验证这样的集合

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);
    }
  }
};
于 2014-03-25T20:34:52.240 回答