在下面的场景中。我正在调用getRole函数,该函数在不同的js.It应该返回json对象。但是问题看起来就像这里有一个并行线程在下面的代码中运行。所以预期的结果不会到来在下面的范围内。我希望getRole函数首先完成并返回结果并分配到下面的范围。
$scope.callFilter=function(result){
$scope.resultObject=getRole();
};
下面的是getRole
function getRole(){
//intranetId = document.getElementById("userId").value;
intranetId ="TAPAS.BANDYOPADHYAY@IN.IBM.COM";
var valuesArray = "";
var valuesArray = $('input:checkbox:checked').map( function() {
return this.value;
}).get().join(",");
alert(valuesArray);
var invocationData = {
adapter : 'RoleAdapter',
procedure : 'getRoles',
parameters :[intranetId,'all',valuesArray]
};
var options = {
onSuccess : getRoleSuccess,
onFailure : getRoleFailure,
};
WL.Client.invokeProcedure(invocationData, options);}
function getRoleSuccess(result){
//alert("inside getRoleSuccess");
var httpStatusCode = result.status;
if (200 == httpStatusCode) {
var invocationResult = result.invocationResult;
var isSuccessful = invocationResult.isSuccessful;
if (true == isSuccessful) {
var text_json = invocationResult.text;
var json_parse = JSON.parse(text_json);
alert("json"+text_json);
role=json_parse;//JSON.parse(int);
var elementExist=document.getElementById("myForm");
if(elementExist!== null){
document.getElementById("myForm").submit();
}else{
return text_json;
}
}
else {
alert("Error. httpStatusCode=" + httpStatusCode);
return false;
}
}
else{
alert("http Failure");
return false;
}}
答案 0 :(得分:0)
问题很明显。你的getRole方法没有返回任何东西。它正在调用异步操作并立即返回
WL.Client.invokeProcedure(invocationData, options);
可能你可以稍微重构你的代码以实现你的目标。使用回调,
请注意,您使用的getRole方法不是最佳方法,它不处理故障和其他错误情况。
$scope.callFilter=function(result){
getRole(function(result){
$scope.resultObject= result;
$scope.$apply(); // so that angular is aware value changed
});
};
和
function getRole(callback){
//intranetId = document.getElementById("userId").value;
intranetId ="TAPAS.BANDYOPADHYAY@IN.IBM.COM";
var valuesArray = "";
var valuesArray = $('input:checkbox:checked').map( function() {
return this.value;
}).get().join(",");
alert(valuesArray);
var invocationData = {
adapter : 'RoleAdapter',
procedure : 'getRoles',
parameters :[intranetId,'all',valuesArray]
};
WL.Client.invokeProcedure(invocationData, {
onSuccess : function (result){
var httpStatusCode = result.status;
if (200 == httpStatusCode) {
var invocationResult = result.invocationResult;
var isSuccessful = invocationResult.isSuccessful;
if (true == isSuccessful) {
var text_json = invocationResult.text;
var json_parse = JSON.parse(text_json);
alert("json"+text_json);
role=json_parse;//JSON.parse(int);
var elementExist=document.getElementById("myForm");
if(callback) {
callback(role);
}
if(elementExist!== null){
document.getElementById("myForm").submit();
}else{
return text_json;
}
}
else {
alert("Error. httpStatusCode=" + httpStatusCode);
return false;
}
}
else{
alert("http Failure");
return false;
}},
onFailure : getRoleFailure
};
);
}