我在通过以下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
}
如您所见,callDef首先调用function2,返回延迟,完成执行,然后将结果从function2的dataStructure
对象传递到function3。这一切在延迟/异步行为方面都很好,问题是被调用的dojo/_base/lang.hitch
函数没有像往常那样将范围从函数传递给函数,在这种情况下从callDef
到{ {1}}。我还希望将相同的范围传递给function2
。我的require语句是正确的,我有其他非同步function3
调用,它们在同一个小部件/文件中成功。
感谢您的帮助
答案 0 :(得分:2)
您可能正在寻找
…
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