当变量的属性发生更改时,我试图记录一个错误,但验证从未被触发,这是我的代码:
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'});
我什至试过这样调用 set:
person.set({name: 'blank'}, {validate: true});
但这也不起作用,我使用的是 1.0.0 版。