[英]Dojo: Correct way to 'Hitch' scope of deferred types
I am experiencing difficulty passing scope through the following Dojo widget's asynchronous structure: 我在通过以下Dojo小部件的异步结构传递范围时遇到困难:
function callDef(){
//This function has the scope I need
var deferred = new Deferred();
//try to hitch 'this' to function2
DojoBaseLang.hitch(this,deferred.resolve(function2(1)));
deferred.then(DojoBaseLang.hitch(this, function(callback) {
callback.then(
function (desiredResult) {
//How to hitch callDef initial scope to function3?
function3(desiredResult);
},
function (err) {
// Do something when the process errors out
console.log(err);
})
}),
function (err) {
// Do something when the process errors out
console.log(err);
}
);
function function2(variable){
//callDef scope not passed by hitch :(
var dataStucture;
//deferredFunction is a function which returns type Deferred
return deferredFunction(hierarchyTableQuery, function(dataSet){
//some iterative maniupulations will be performed on dataStructure here
dataStructure = dataSet;
}).then(function (){
return dataStructure;
});
}
function function3(variable){
//need a way to also have scope in this method
//doing other stuff
}
As you can see, callDef first calls into function2, returns a deferred, finishes execution and then passes the result from function2's dataStructure
object into function3. 如您所见,callDef首先调用function2,返回一个延迟的函数,完成执行,然后将结果从function2的dataStructure
对象传递给function3。 This all works fine in terms of Deferred/Async behavior, the problem is that the dojo/_base/lang.hitch
function being called is not passing the scope from function to function as it usually does, in this case from callDef
to function2
. 就Deferred / Async行为而言,这一切都很好,问题在于被调用的dojo/_base/lang.hitch
函数没有像通常那样将范围从函数传递到函数,在这种情况下是从callDef
到function2
。 I would also like to pass the same scope into function3
. 我也想将相同的作用域传递给function3
。 My require statement is correct, and I have other non-async .hitch
calls which are successful in the same widget/file. 我的require语句是正确的,并且我还有其他非异步.hitch
调用,它们在同一小部件/文件中均成功。
Thank you for any assistance 感谢您的协助
You're probably looking for 您可能正在寻找
…
deferred.resolve(DojoBaseLang.hitch(this,function2));
deferred.then(DojoBaseLang.hitch(this, function(callback) {
callback(1).then(
DojoBaseLang.hitch(this,function3),
function (err) {
… // rest of the code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.