Atlas Hello World
Dflying | 11 March, 2006 23:42Let's begin with the classic ‘Hello World!’ example.
The HelloWorld.aspx page has one button. When the button is clicked, data is retrieved from the server and displayed in an alert box.
One of the core features of Atlas is getting data form web server by asynchronous calls. In this example we'll see how to make it, that is, how to retrieve data from the server without doing a traditional ASP.NET PostBack. To do this, we can invoke a server method from client code.
Using Atlas, we can- Invoke a method defined in an .aspx page.
- Invoke a method defined in a Web Service
In this hello world, I just show the Web Service one, which should also be recommended I think, for a Web Service page is only used for getting pure data, not the masses wrapped by the presentation HTML tags, and then client side JavaScript should be responsible for rendering these to user. When the button is clicked, a method HelloWorld() exposed in the Web Service is called. This method returns a string that is displayed in an alert box.
Here is the HelloWorld.aspx page<div>
<input type="button" value="Hello from Page" onclick="sayHello();" />
</div>
<script type="text/javascript">
function sayHello() {
MyWebService.HelloWorld(onComplete);
}
function onComplete(result) {
alert(result);
}
</script>
The sayHello() JavaScript function makes a call to the HelloWorld() server methods. Atlas helps us in doing this by defining proxy classes. A proxy class exposes a server method through a client function with the same name of the server method. In other words, proxy classes allow calling server methods "as if they were client methods".
For a Page class, if it contains web methods (method marked as [WebMethod]), a proxy class named PageMethods is generated at runtime. For a web service, a proxy class is generated with the name of the web service. The proxy class for a web service resides in a file with the extension .asmx/js, that must be imported in the page as a script to be available. We can do this by dropping the declaration in html <head>section.
<atlas:ScriptManager ID="scriptManager" runat="server"> <Services> <atlas:ServiceReference Path="MyWebService.asmx" /> </Services> </atlas:ScriptManager>
The ScriptManager is a server control that is responsible to add the needed Atlas libraries and to import WebService proxies and JavaScript external files.
The above declaration adds a reference to MyWebService.asmx, which is assumed to reside in the root directory of the application (note the Path attribute). References to Web Services are enclosed in the <Services> section, while references to external JavaScript files are declared in the <Scripts> section. Finally, references to the main Atlas libraries (the core library and the compatibility layers) are added automatically at runtime so there is no need to explicitly declare them, while an explicit declaration is needed for certain Atlas control libraries (DragDrop controls or UI visual effects).
We were talking about proxy classes. Functions defined in a proxy class accept the same arguments as the corresponding server methods, plus additional references to functions that are responsible to process the result of the asynchronous call to the server method.
MyWebService.HelloWorld(onComplete);
The HelloWorld() method accepts no arguments, but when invoking it through the proxy class we specify a function onComplete() to be called when the server method returns. With this function, we can access the data returned by the server method through its first argument:
function onComplete(result) { alert(result); }
The above function displays in an alert box the data returned from the HelloWorld() method. In a similar way, when calling methods from a proxy class, we can specify other functions to be invoked when the asynchronous call completes (typically a function to be called if an error occurred, another to be called if a timeout occurred).
Then, the server side function in Web Service is.
[WebMethod] public string HelloWorld() { return "Hello World from MyWebService!"; }
That is really a simple Web Service and no more words on that.
Till now, everything’s done. Just press F5, and good luck :-)
Posted in
Atlas.
Comment: (4).
Trackbacks:(596).
Permalink
«Next post |
Previous post»
Referers
Comments
-
1. blueoxygen | 03/12,2006 at 17:27
All ajax Frameworks are likeness besides atlas.In java world,there is also a framwork called DWR,works as atlas.But it will centralize all service Classes config in a xml file in class path.
I think atlas also can ,right?
I wonder,if the webservice are on remote server,can atlas do cross-domain invoking (generate gateway proxy class automatically)? -
2. Dflying | 03/12,2006 at 18:32
Cross-domain invoking is not the feature provided by framework but by the implementation of XmlHttpRequest. IE use ActiveX with cross domain support but other browsers use XmlHttpRequest, which disabled it.
-
3. blueoxygen | 03/12,2006 at 20:58
I know,IE will popup a alert window to ask user's option on cross domain request,and FireFox will deny the request by default.
If i wanna retrieve data from Yahoo!'s web service,i usually create a proxy class on server side as a gateway,and XHR request that proxy class.The class comunicate with web service interface.I mean,whether atlas can do this for developer? -
4. Dflying | 03/12,2006 at 21:10
Nothing to do, I think.




