延迟函数解析
这是我的情况:
我有一个“界面”,我的每个控件都使用它来执行基本操作,其中之一就是验证。
因此,我有一个 processValidation 函数,它运行该特定控件的每个传入函数。这些函数可能像 isNumeric()
一样简单,也可能更复杂,需要 Web 服务调用。这些函数返回一个简单的布尔值,说明是否通过了验证。
我需要一种通用的方法来让这个调用等待,直到它正在运行的验证完成。我认为这是使用延迟方法的完美场所,但我似乎无法做到正确。
这是我到目前为止所得到的:
var dfd = $.Deferred(function (dfd) {
validator.validatorFn(value, $controlContainer);
}).promise();
$.when(dfd).done(function (result) {
console.log('got here');
});
当我进入被调用的函数时,我需要一种方法来解析 dfd。我想这才是我真正的问题。
想法?
编辑: 我尝试将 dfd 传递给 validatorFn 并在那里解析它,但 $.when 从未触发。
Here is my situation:
I have an "interface" that each of my controls use for basic things, one of those things is validation.
So I have a processValidation
function which runs through each of the passed in functions for that particular control. Those functions might be as simple as isNumeric()
or more complex requiring a webservice call. These functions return a simple boolean
stating whether or not this passed validation.
I need a generic way to have this call wait until that validation it is running finishes. I thought this was a perfect place for using Deferred methods, but I can't seem to get it right.
Here is what I have so far:
var dfd = $.Deferred(function (dfd) {
validator.validatorFn(value, $controlContainer);
}).promise();
$.when(dfd).done(function (result) {
console.log('got here');
});
When I get into the function being called I need a way to resolve the dfd. I guess that is my real problem.
Thoughts?
EDIT:
I tried passing dfd
to the validatorFn and resolving it there but the $.when
never fires.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定您的流程,但为什么不让
validator.validatorFn
返回延迟对象呢?类似于:然后:
DEMO
I'm not really sure about your flow, but why not let
validator.validatorFn
return the deferred object? Something like:Then:
DEMO