骨干验证和回调
Backbone validation and callback
谁能帮助我,Javascript采取函数'检测'未定义,我们怎么能从回调返回值检测函数?
validate: function (attrs, options) {
if (!this.detect(attrs.selectedFile, this.onComplete)) {
return "this is an error message";
}
},
detect: function (file, callback) {
var attributes = this.attributes,
image = new Image();
image.onload = function () {
if (condition_is_false_return_false) {
callback(false);
} else {
callback(true);
}
};
image.src = URL.createObjectURL(file);
},
onComplete: function (value) {
return value;
}
代码中有两个问题。1. validate函数只验证attr候选者,这里的"this"对象有歧义,它可能不是模型。因此this.detect没有定义。你可能需要在下划线中使用bind函数
- 从技术上讲,在设置属性之前运行validate函数。即使你能成功地调用detect函数,在它里面,当你这样做的时候。属性时,您将得到一个空集。所以你什么都探测不到。最好将这些函数设置为静态,这意味着它们不引用"this" 。
我做了一个jsfiddle,
var myOwnModel=Backbone.Model.extend({
initialize:function(model,options){
Backbone.Model.prototype.initialize.apply(this,arguments);
},
validate: function (attrs, options) {
console.log("doing validate",attrs);
if (!this.detect(attrs.selectedFile, this.onComplete)) {
return "this is an error message";
}
},
detect: function (file, callback) {
var attributes = this.attributes,
image = new Image();
console.log("running detect",attributes) // Here attributes does not have the new attr.
image.onload = function () {
if (condition_is_false_return_false) {
callback(false);
} else {
callback(true);
}
};
image.src = URL.createObjectURL(file);
},
onComplete: function (value) {
return value;
}
})
var a=new myOwnModel({a:1})
a.fetch();
// or test it with
a.set({b:1},{validate:true});
console.log(a)
看起来验证确实有访问"this"的权限。并且检测功能成功运行。那么问题是什么呢?
相关文章:
- PassportJS验证回调完成(xx,xx..),它已经声明还是我需要声明自己
- 修复了在 PhoneGap 中使用 jQuery Mobile 的 jQuery 验证中导致错误错误的延迟回调
- 护照身份验证回调而不是重定向
- 语义 UI 在成功时,在表单验证时未触发失败回调
- 护照身份验证在回调时做什么
- jQuery 验证需要回调
- 即使表单验证失败,也会调用语义UI onSuccess回调
- 身份验证-$http'调用了s.then()成功回调,而不是错误回调
- 在AngularJS中,在控制器之间共享数据,并添加用于身份验证的回调行为
- JavaScript 表单验证库,支持具有灵活回调的自定义规则
- 如何在javascript中实现push存在身份验证回调
- 护照身份验证回调挂起
- 骨干验证和回调
- 如何使用这个表单验证(onSuccess回调)
- 在Yii2中表单验证后的回调Javascript函数
- 如何使用窗口.Onload函数分配一个验证回调函数
- React表单验证——回调而不是事件
- 未定义不是函数:单元测试护照身份验证自定义回调
- 节点快速通行证(JWT) -验证后回调
- 带有回调的JSON模式解析器/验证器