我实现了这个简单的功能来检测Backbone模型中的任何不需要或未指定的属性:
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;
我之所以这样做是因为我在Backbone.validation中找不到任何替换它的东西。 我怎么能用首选的方法替换这个代码来使用Backbone.validation(github thederson.com)?
感谢。
答案 0 :(得分:1)
我还没有找到backbone.validation
中的方法,但您可以更轻松地编写代码:
isAttributeAccepted: function(attr) {
return _.has(this.validation, attr);
},