Wednesday, March 28, 2012

Accessing variables from OnComplete

Hello guys, we've been trying to convert our web app to Atlas from AJAX.NET and ran into a bit of a problem with the way our AJAX calls are structured.

Take, for example, this hypothetical function:

function getAlbumName(aID)
{
PlanetEye.GTTS.AjaxService.getAlbumName(aID, OnComplete)
}
function OnComplete(result)
{
document.getElementById("albumDiv").innerHTML =result;
}
Now, my question is: Is there a way to access the aID variable from within the OnComplete function? For example, if I want the innerHTML to read something like -"id: "+aID+" name:"+result

Back when we were using AJAX.NET we avoided the call back function all together so that we could just say:albumName =PlanetEye.GTTS.AjaxService.getAlbumName(aID);and continue on with the getAlbumName function, but i dont' think that's possible with Atlas, is it?

Thanks for any help, or pointers in the right direction Smile [:)]

Yep, you can use the userContext parameter which gets passed into the OnComplete callback, there's a few ways to specify the userContext, its probably best for me to just point you at the docs,

http://atlas.asp.net/docs/atlas/doc/services/consuming.aspx#alt

I believe your OnComplete will need to look like this once you update the call:

OnComplete(result, response, userContext)...

And you would just need to pass your aID as your userContext...

Hope that helps,
-Hao


HaoK:

Yep, you can use the userContext parameter which gets passed into the OnComplete callback, there's a few ways to specify the userContext, its probably best for me to just point you at the docs,

http://atlas.asp.net/docs/atlas/doc/services/consuming.aspx#alt

I believe your OnComplete will need to look like this once you update the call:

OnComplete(result, response, userContext)...

And you would just need to pass your aID as your userContext...

Hope that helps,
-Hao

This part motivated me to research a bit more about this in the search for the solution my needs. I was used to AJAX.NET in the way that I were able to get a result without having to write a callback function. For example (pseudo code):

1// Function called from any button2function PerformTasks(controlid, 2ndParam)3{4 // Ajax call5 res = AjaxMethods.GetResults(2ndParam);67 // Do the stuff needed8 document.getElementById(controlid).innerHtml = res.value;910 // Other logic11}

Using new WebMethod logic we need to pass parameters to the OnComplete function. We can do this using one value ou several becauseuserContext can be any JavaScript primitive type, array, or object. So consider this:

1// My example function called from an OnCLick event2function CommentFile(sFilename, drpClientID)3{4var arrUserContext = new Array();5arrUserContext[0] = sFilename;6arrUserContext[1] = drpClientID;78EntitiesService.GetFileNotes(sFilename, OnCommentFile, OnCommentFailure, arrUserContext);91011}1213// The OnComplete function14function OnCommentFile(arg, arrUserContext)15{16var sFilename = arrUserContext[0];17var drpClientID = arrUserContext[1];1819drpTmp = document.getElementById(drpClientID);20drpRevisionValue = drpTmp.options[drpTmp.selectedIndex].value;2122alert(arg);2324// all the logic from here, we can use the return arg and the param(s) from the source function that is triggered from any link, etc.2526}2728// The OnFailure function29function OnCommentFailure()30{31// Empty32}
My problems are solved with this solution.

No comments:

Post a Comment