问题描述
我知道有这个几个线程,但我认为SAPUI5方面没有线程的答案在SAPUI5延迟/同步调用这个一般性的主题。
在我的控制器我:
I know there are several threads on this, but I think in SAPUI5 context no thread answers this general topic on deferred/sync calls in SAPUI5.
In My Controller I got :
test : function() {
var dfd = $.Deferred();
var sServiceUrl = '/sap/opu/odata/sap/xyz/MySet?$format=json';
var post = $.ajax({
url: sServiceUrl,
type: "GET"
});
post.done(function(data){
console.log(data);
dfd.resolve();
});
post.fail(function(){
console.log("Error loading: " + sServiceUrl);
dfd.reject();
});
return dfd.promise();
},
在我看来,我打电话的方法,我想等待结果,怎么我正确地管理它?
in My view I am calling the method AND I want to wait for the result, how to I manage it correctly?
var test = oController.test();
console.log(test);
$.when(test).done().then(console.log("finished"));
而且,这种方法不会等待:
also this approach does not wait:
$.when(oController.test()).then(console.log("finished"));
正如预期的那样,测试是不确定的,完成的记录,而当.done的方法是准备好了,它被记录下来。但我想等待它(在从阿贾克斯回最好的情况下返回数据)..
我怎么等待post.done()在我看来,是否继续?
As expected, test is undefined, "finished" is logged, and when .done from method is ready, it is logged. but I want to wait for it (and in best case return data from ajax back)..
how do I wait for post.done() to continue in my view?
推荐答案
()
运营商调用函数。你是你自己调用该函数,该函数不叫由然后
方法。会发生什么事是你叫日志
函数,它的返回值被设置为处理程序。既然你想传递一个参数传递给的console.log
方法,你可以使用匿名函数:
()
operator invokes the function. You are invoking the function yourself, the function is not called by the then
method. What happens is you call the log
function and it's returned value is set as the handler. Since you want to pass an argument to the console.log
method, you can use an anonymous function:
dfd.resolve(data);
// ...
$.when(oController.test()).then(function(data) {
console.log('finished', data);
});
这篇关于SAPUI5等待延迟,对象//等待.done()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!