我正在使用主干验证功能,但它再次给出此错误 Uncaught TypeError: Object [object Object] has no method 'apply'
。我真的不知道为什么会给我这个错误。
这是我的代码
$(function () {
var Veh = new Backbone.Model.extend({
validate: function (attrs) {
var validColors = ['white','red', 'black'];
var colorIsValid = function (attrs) {
if(!attrs.color) return true;
return _(validColors).include(attrs.color);
}
if(!colorIsValid(attrs)) {
return 'wrong color';
}
}
});
var car = new Veh();
car.on('error', function (model, error) {
console.log(error);
});
car.set('foo', 'bar');
});
最佳答案
更新:错误是使用了new。使用 Backbone.extend 时不要做 new。这是因为您创建的是类而不是对象。
$(function () {
var Veh = Backbone.Model.extend({
validate: function (attrs) {
var validColors = ['white','red', 'black'];
var colorIsValid = function (attrs) {
if(!attrs.color) return true;
return _(validColors).include(attrs.color);
}
if(!colorIsValid(attrs)) {
return 'wrong color';
}
}
});
通知
var Veh = Backbone.Model.extend
而不是
var Veh = new Backbone.Model.extend
参见 this关于无意中使用 new 的副作用的问题。
关于javascript - 主干验证功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12996898/