我是骨干新手。
var Person = Backbone.Model.extend({
defaults: {
name: '',
age: 30,
occupation: 'fireman'
},
validate: function(attrs) {
console.log(attrs) // this won't trigger? since the model's attr changed;
},
run: function() {
return this.get('name') + ' is running';
}
})
var p = new Person({name:'James'});
p.set('age',25);
我更改了模型的属性,但不知怎的,我没有看到控制台。为什么?
答案 0 :(得分:2)
您需要通过传递validate选项来告诉set方法验证。 http://backbonejs.org/#Model-validate
p.set({age: 25}, {validate: true});