webOS skill level: Beginner
Technologies covered: Ajax
Ajax and webOS
First off: What is Ajax? From wikipedia:
Ajax...is a group of interrelated web development techniques used to create interactive web applications or rich Internet applications. With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page.
Basically, if your goal is to create an interactive web page that makes use of remote data (information that does not reside on the user's local device) and responds to user input without reloading a new page, you want to use Ajax. But what does this mean for webOS developers?
Using Ajax in your webOS application can be distilled into the following 3 steps:
- Create a page on your webserver that is designed to process Ajax requests made from your application and return the requested data.
- Process the returned data and display it to the user.
If you find this example confusing or could use some additional clarification, this site has posted the same tutorial with different commentary, as well as a server-side script written in PHP instead of ASP:
Both tutorials detail the usage of an ActiveX object for IE browsers; you can safely ignore any references to the ActiveX object as webOS will not be using it. (ActiveX is a proprietary Microsoft technology.)
One thing I want to call out in these tutorials: the responseText property of the XMLHttpRequest object contains whatever you want to return from your page. That could be XML, JSON, or data formatted specifically for use in your application. For example, I created an application that would populate drop-down menus on an HTML form automatically. So the data I returned with my script was in the format:
and I plugged this directly into my HTML element; no messy XML parsing required.
Also note that some browsers currently restrict XMLHttpRequest calls from communicating with servers that are on a different domain than the one from which the web page was loaded. In the case of webOS, the application's scene will be loaded from the Pre's internal "server", so if this restriction were to be in effect, it would result in no applications being able to use XMLHttpRequest to retrieve data from a web server. We'll have to see how Palm handles this. They may require an application to specify the web server(s) with which it will communicate and/or require that the webserver somehow authorize that specific application to retrieve data from it. Or, they may leave it wide open and allow any application to request data from any webserver.
- Frameworks like Prototype are usually open-source and community-built, so it's like having thousands of people looking at a part of your application to make sure bugs are found and fixed.
With all those benefits, why would you not want to use a framework like Prototype? There are a few possibilities:
- Increased memory usage. When you use a framework, your application must load the library files of that framework into memory before it can utilize them. The effect of this increased memory usage may be negligible on the Pre or other webOS devices; we won't know for sure until we can get our hands on one. It may also be negated by the fact that if another, already loaded application is using the framework, your application can take advantage of it without a memory penalty as it's already loaded into memory for the other application.
- You want minute control over the details. Since a framework like Prototype is designed to take care of the details for you, if you want to tweak a very specific aspect of your Ajax transfer, you may find that the framework functions don't give you enough control.
If you weigh the benefits and drawbacks and decide using a framework is for you, let's look at the Ajax and the Prototype framework!
Once again, I'm going let defer to an already excellent tutorial provided by the folks over at Prototypejs.org:
using the Prototype framework: http://www.theregister.co.uk/2006/12/17/ajax_prototype_tutorial/
Ajax and JSON
SeanBlader from the forums pointed out that JSON is a data interchange method that has some advantages over XML. From what we've seen so far from Palm, JSON plays a big part in webOS development, being used in the first official webOS tutorial and some of the supporting material in the recent developer webcast.
Here's a great article on the advantages of using JSON over XML with Ajax:
or in SeanBlader's own words from the forums:
That should give you enough to get started with Ajax on webOS. If you combine what you've learned in this tutorial with our HTML5 Database Storage tutorial, you can figure out how your application could get data from a remote server and store it locally on the Pre or any other webOS device for offline use.
When Palm releases new information or best practices for remote data transfers in webOS, we'll come back and update this article.