Deferred function resolution
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 r开发者_运维问答eal problem.
Thoughts?
EDIT:
I tried passing dfd
to the validatorFn and resolving it there but the $.when
never fires.
I'm not really sure about your flow, but why not let validator.validatorFn
return the deferred object? Something like:
validator.validatorFn = function(value, controlContainer) {
var df = $.Deferred();
// do validation
// somewhere you call
df.resolve(result);
// or maybe df.reject(result);
return df;
};
Then:
$.when(validator.validatorFn(value, controlContainer)).done(function (result) {
console.log('got here');
});
DEMO
精彩评论