gpt4 book ai didi

javascript - 为什么不会验证触发错误-backbone.js

转载 作者:行者123 更新时间:2023-11-28 02:25:20 24 4
gpt4 key购买 nike

为什么验证不会触发错误,因为“fred”应该使验证条件在设置时返回 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() 在保存时被调用,但在设置属性时不会被调用,除非您明确告诉它这样做。来自 docs :

By default validate is called before save, but can also be called before set if {validate:true} is passed.

关于javascript - 为什么不会验证触发错误-backbone.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15054871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com