Reputation: 1470
I implemented this simple feature to detect any undesired or unspecified attributes in a Backbone Model:
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;
},
...
In use:
var user = new User();
var isDesired = user.isAttributeAccepted( "nop" );
console.log( isDesired ) // false;
I only done it because I cannot find anything in the Backbone.validation to replace this. How could I replace this code with a prefered way to use Backbone.validation (github thederson.com) ?
Thanks.
Upvotes: 1
Views: 81
Reputation: 1018
I haven't found methods in backbone.validation
to do it, but you can write your code a little bit easier:
isAttributeAccepted: function(attr) {
return _.has(this.validation, attr);
},
Upvotes: 1