2013-06-24 89 views
0

我想记录一个错误工作时可变的属性发生变化,但验证永远不会被触发,这是我的代码:验证功能没有在骨干

Person = Backbone.Model.extend({ 
     initialize: function() { 
      console.log("hello world"); 
      this.bind('change:name', function() { 
       console.log(this.get('name') + ' is now the value for the name'); 
      }); 
      this.bind('error', function(model, error) { 
       console.log(error); 
      }); 
     }, 
     defaults: { 
      name: 'Bob Hope', 
      height: 'unknown' 
     }, 
     validate: function(attributes) { 
      if(attributes.name == 'blank'){ 
       return 'no'; 
      } 
     } 
    }); 


var person = new Person(); 
person.set({name: 'blank'}); 

我甚至试试这样设置:

person.set({name: 'blank'}, {validate: true}); 

但这也行不通,我使用的是1.0.0版本。

回答

1

the documentation

默认情况下验证之前保存调用,但也可以设置调用之前 如果{验证:真}传递。

而且,所触发的事件invalid,不error,那么试试这个:

this.bind('invalid', function(model, error) { 
    console.log(error); 
});