Geeks With Blogs
Jonas Bush Blog.blog.blog.blog

One of the things I've been working on is how to use Atlas web services to verify actions on the server. So the client requests to perform a particular action on the server, and if they don't have access to perform that action or the action fails (because a database is down, or something along those lines), I don't want the method on the client to continue executing.

The problem with that is that Atlas web service calls are asynchronous by nature (explained here and here). So I can't say something like this:

if(myService.myServiceCall() == true)
{
  //do stuff
}

What I've come up with is this javascript object:

function webServiceRunner(webServiceFunction, returnFunction, returnParams)
{
 
var _returnFunction = returnFunction;
 
var _returnParams = returnParams;
 
var _webServiceFunction = webServiceFunction;

  this
.runService = function
()
 
{
   
_webServiceFunction(onServiceComplete);
 
}

  function onServiceComplete(result)
 
{
   
var r = '_returnFunction(';
   
for(var i = 0; i < _returnParams.length; i++)
   
{
     
r +=
'_returnParams[' + i.toString() + ']';
     
if(i < returnParams.length - 1)
       
r +=
',';
   
}
   
r +=
', result);';
   
//alert(r);
   
eval(r);
  
}
}

This takes advantage of some of the nice (and dangerous) features of Javascript, the eval() statement, the arguments object, and function pointers. Eval() will let you create a string, like var s = 'alert(“hi“);'; - and then call eval(s); - the end result of which will be “Hi“ showing up in an alert box. The arguments object is created automatically for you in javascript inside of any function, and contains an array of all the arguments passed into that function.

So now I can instantiate and call this class like so:

<span class="CommandLink" onclick="testOne(this);">Testspan>

function testOne(sender, result)
{
 
if(result == undefined)
 
{
   
var sr = new webServiceRunner(TestWebService.HasAccess, testOne, arguments);
   
sr.runService();
   
return;
 
}

 
if(result)
 
{
 
}
 
else
 
{
   
alert(
'sorry, you are unable to perform that action');
 
}
}

Assuming I have a webservice defined, as well as the proper Atlas script manager setup:

<atlas:scriptmanager id="scriptManager" runat="server">
 
<services>
   
<atlas:servicereference path="TestWebService.asmx" />
 
services>
atlas:scriptmanager>

[WebMethod]
public bool
HasAccess()
{
 
return false;
}

What ends up happening inside the webServiceRunner is that the following string will be created:

_returnFunction(_returnParams[0], result);

Then that string is eval-ed, and my original testOne function is called, with the original sender object (from the original click), and the result parameter from the web service.

If this is confusing, post a comment or send me an email and I'll try and elaborate further. If you think this is silly or useless, leave a comment and explain why. :)

 

Posted on Wednesday, August 16, 2006 2:48 PM Atlas , ASP.Net | Back to top


Comments on this post: Using Atlas to act as a check on javascript calls

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Jonas Bush | Powered by: GeeksWithBlogs.net