我实现了此简单功能,以检测骨干模型中任何不需要的或未指定的属性:

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)替换此代码?

谢谢。

最佳答案

我还没有在backbone.validation中找到方法来做到这一点,但是您可以编写一些简单的代码:

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

关于node.js - 检测 Backbone 模型验证是否有任何不需要的属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22648482/

10-16 14:16