Javascript 为什么不激活错误-backbone.js

Javascript 为什么不激活错误-backbone.js,javascript,backbone.js,Javascript,Backbone.js,为什么validate不会触发错误,因为“fred”应该在设置时使validate条件返回true Person = Backbone.Model.extend({ initialize: function () { console.log('inisialize Person'); this.bind("change:name", function () { console.log(this.get('name') + ' is

为什么validate不会触发错误,因为“fred”应该在设置时使validate条件返回true

Person = Backbone.Model.extend({

    initialize: function () {
        console.log('inisialize Person');
        this.bind("change:name", function () {
            console.log(this.get('name') + ' is now the name value')

        });
        this.bind("error", function (model, error) {

            console.log(error);

        });
    },
    defaults: {
        name: '',
        height: ''
    },
    validate: function (attributes, options) {  

        if (attributes.name == "fred") { //why wont this work?

            return "oh no fred is not allowed";
        }

    }

});

//var person = new Person({ name: 'joe', height: '6 feet' });
var person = new Person();
person.set({ name: 'fred', height: '200' });
保存时会调用validate(),但在设置属性时不会调用,除非您明确告诉它这样做。从:

默认情况下,在保存之前调用validate,但也可以调用validate 如果传递了{validate:true},则在设置之前

保存时会调用validate(),但在设置属性时不会调用,除非您明确告诉它这样做。从:

默认情况下,在保存之前调用validate,但也可以调用validate 如果传递了{validate:true},则在设置之前


尝试以下操作:在
initialize()
中,将
this.bind('error')
更改为
this.on('invalid')
上的
'error'
事件用于调用save()后服务器的故障。
“无效”
用于客户端的验证错误。最后,添加
{validate:true}
作为
person.set()调用的第二个参数。默认情况下,主干不会验证
set()

Person = Backbone.Model.extend({
    defaults: {
        name: '',
        height: ''
    }, 

    validate: function(attributes) {
      if(attributes.name === 'fred' )
        return 'oh no fred is not allowed';
    },

    initialize: function() {
        alert('welcome');
        this.on('invalid', function(model, error){
          alert(error);
        });
        //listeners
        this.on('change:name', function(model) {
            var name = model.get('name');
            alert('changed ' + name);
        });

});

var person = new Person();
person.set({ name: 'fred', height: '200' }, {validate: true}); //oh no fred is not allowed

尝试以下操作:在
initialize()
中,将
this.bind('error')
更改为
this.on('invalid')
上的
'error'
事件用于调用save()后服务器的故障。
“无效”
用于客户端的验证错误。最后,添加
{validate:true}
作为
person.set()调用的第二个参数。默认情况下,主干不会验证
set()

Person = Backbone.Model.extend({
    defaults: {
        name: '',
        height: ''
    }, 

    validate: function(attributes) {
      if(attributes.name === 'fred' )
        return 'oh no fred is not allowed';
    },

    initialize: function() {
        alert('welcome');
        this.on('invalid', function(model, error){
          alert(error);
        });
        //listeners
        this.on('change:name', function(model) {
            var name = model.get('name');
            alert('changed ' + name);
        });

});

var person = new Person();
person.set({ name: 'fred', height: '200' }, {validate: true}); //oh no fred is not allowed

谢谢-看起来我需要添加person.set({name:'fred',height:'200'},{validate:true});并且错误必须更改为无效this.bind(“无效”,函数(模型,错误){console.log(错误);})@你好,世界对了。在doc示例中,他侦听模型实例上的“无效”状态。谢谢-看起来我需要添加person.set({name:'fred',height:'200'},{validate:true});并且错误必须更改为无效this.bind(“无效”,函数(模型,错误){console.log(错误);})@你好,世界对了。在doc示例中,他侦听模型实例上的“无效”状态。