Tuesday, 10 September 2013

Variable Scope Dojo xhrPost

Variable Scope Dojo xhrPost

DISCLAIMER - I've reviewed the existing SO entries and cobbled together
something that should work but still does not.
I have the following function. Basically it is sending a pair of values to
a webservice with the results coming back in JSON:
getPicklist: function () {
var xhrArgs = {
url: 'myUrl',
postData: dojo.toJson({
'opportunityId': 'myOppId',
'loggedInUserId': 'myUserId' //App.context.user.$key
}),
headers: {
"Content-Type": "application/json"
}
}
var deferred = dojo.xhrPost(xhrArgs);
deferred.then(
function (data) {
var jsonResponse = dojo.fromJson(data);
picklistName = jsonResponse.PicklistName;
if (!picklistName) {
picklistName = "defaultPickListName";
}
return picklistName;
},
function (error) {
alert("Could not load picklist " + error);
});
;
//return picklistName; -- null
}
My understanding after reading this: anonymous js function with xhrpost
dojo not returning data
Was that adding a variable outside of this function scope, along with
using dojo.deferred, would fix the issue. I tried placing a var outside of
the function, and assigned the object to the picklistName variable.
However, I still cannot get this function's result (the picklistName
variable).
Can someone please clarify what I am doing wrong, and how I can fix it?

No comments:

Post a Comment