检测任何不需要的属性的Backbone模型validation

我实现了这个简单的function来检测骨干模型中的任何不需要或未指定的属性:

var Underscore = require( '/usr/local/lib/node_modules/underscore' ), Backbone = require( '/usr/local/lib/node_modules/backbone' ), Validation = require( '/usr/local/lib/node_modules/backbone-validation' ); Underscore.extend( Backbone.Model.prototype, Validation.mixin ); var User = Backbone.Model.extend( { validation: { firstname: { minLength: 1, maxLength: 20, required: true }, lastname: { minLength: 1, maxLength: 20, required: true } }, ... isAttributeAccepted: function( attr ) { var retval = false; for ( var property in this.validation ) { if ( attr == property ) { retval = true; break; } } return retval; }, ... 

正在使用:

 var user = new User(); var isDesired = user.isAttributeAccepted( "nop" ); console.log( isDesired ) // false; 

我只做到这一点,因为我无法findBackbone.validation中的任何东西来取代它。 我怎么可以用一个喜欢的方式来代替这个代码来使用Backbone.validation(github thederson.com)?

谢谢。

我没有在backbone.validationfind方法来做到这一点,但是你可以简单的编写你的代码:

  isAttributeAccepted: function(attr) { return _.has(this.validation, attr); },