| By Ajit Jaokar | Article Rating: |
|
| April 6, 2007 10:00 AM EDT | Reads: |
6,190 |
This content is reprinted from Real-World AJAX: Secrets of the Masters published by SYS-CON Books. To order the entire book now along with companion DVDs for the special pre-order price, click here for more information. Aimed at everyone from enterprise developers to self-taught scripters, Real-World AJAX: Secrets of the Masters is the perfect book for anyone who wants to start developing AJAX applications.
AJAX Design Considerations
In the previous section, we laid out the design considerations for mobile Web applications as recommended by the W3C. Here we'll discuss AJAX-specific considerations.
As we know, AJAX is not a new technology. It's a collection of existing technologies put together in a different way. Thus, designing a mobile AJAX application is similar to designing a mobile Web application.
Performance becomes far more critical. While application performance is critical on the Web, it becomes much more severe in a mobile Web environment. Also widget design using AJAX needs some special thought.
Since the synergy between mobile widgets and AJAX is central to our vision, we'll focus on widgets in greater detail. In the next section, we will use some of the principles outlined here to develop some widget code.
To recap, the elements comprising AJAX are
- XHTML and CSS
- Document Object Model
- XML and XSLT
- XMLHttpRequest for asynchronous data retrieval
- JavaScript
Note that you have to consider all the elements discussed in the previous sections (the W3C recommendations) to ensure that the AJAX application works well.
General Browser/Mobile-Level Recommendations
- Don't load more documents than you have to.
- Avoid keeping references alive from one document to another.
- A window's history grows if you keep loading documents into it. When the user moves, the document may not be reloaded and reinitialized.
- Be aware of device limitations like little memory, low-powered CPUs, slow disk access, etc.
- Reflows and repaints are very expensive.
- Timer resolution is unpredictable on a mobile device.
- Trim libraries of unneeded code.
- Minimize use of iframe/object type="text/html".
- When removing an iframe/object, also set any references in script to null.
- Don't use scripts (including event handlers) on documents that don't require them.
- Avoid tight-timed loops whenever possible. setInterval( funcRef, 0) is a no-no, setInterval(funcRef, 50) is okay.
- Carefully evaluate whether you want to use any patterns for adding to the browser history.
- Use single-image rollovers, www.tutorio.com/tutorial/pure-css-image-rollovers.
- Don't use "eval" or the Function constructor if you can avoid it.
- Don't use "with" if you can avoid it.
- Don't use "try-catch-finally" inside performance-critical functions.
- Don't use global variables if you can avoid them.
- Be mindful of implicit object conversion, especially on strings.
- Avoid for-in in performance-critical functions.
- Isolate uses of eval, with, and try-catch-finally inside top-level functions. Almost every use of eval can be rewritten without eval, and the resulting code will be faster and cleaner.
- Use strings accumulator-style: a += "x"; a += "y"; is better than a += "x" + "y".
- Convert primitive values to objects explicitly if you're using them as objects a lot.
- Primitive operations can be faster than function calls: a<b ? a : b instead of Math.min(a,b).
- Keep array objects dense; if you need a hash table, use an object instead.
- Pass functions, not strings, to setTimeout() and setInterval().
- Avoid properties that are re-evaluated in scripts: for (var i = prop.length; i--;) is faster than for (var i=0; i++ < prop.length;).
- Document tree modification will trigger reflow.
- Modifying CSS properties other than backgrounds and colors very likely triggers reflows.
- Don't modify a document while traversing an element collection.
- Avoid manual traversal of the document tree.
- Avoid inspecting large numbers of nodes.
- Timeouts may not be strictly honored.
- Set individual .style properties.
- Don't change Element.id or Element.className.
- Cache DOM values in script variables when you can.
- Make a small number of large changes to the document tree, not a large number of small changes.
- Use document fragments to build subtrees that are to be inserted. Build the entire tree before inserting it.
- Perform modifications by accumulating nodes to be altered during a traversal phase and follow it with a modification phase.
- Screen sizes are relatively predictable, but vary a lot. There are 176, 384, 480, 640 in pixel width.
- Device DPI can vary from 100 to over 200.
- All fonts may not be available.
- CSS selectors affect performance.
- Use CSS3 media queries: @media handheld and (device-width: 384px) { /* Rules for device*/ }.
- Use generic font families.
- Be careful with font sizing. What's readable on a 100dpi-device may be nearly invisible on a 200dpi device.
- Use concise CSS selectors: "div > p" over "div p" if what you mean is "div > p".
- Avoid duplicate CSS rules.
A widget is a small Web application that runs directly on a user's desktop. It is usually focused on doing a single task. Technically developing a widget is similar to developing a Web page except that the widget functions like an application and lives directly on the desktop.
Widgets are chromeless applications and will run without regular user interface elements such as normal browser controls, like the back button or address bar. An example of a widget is the clock widget.
AJAX technology facilitates building widgets. As I've mentioned before, the advantage of widgets (especially when developed using the Opera platform) is that the same code can be used on the desktop, browser, and mobile device with basic modifications.
For purposes of our discussion, we'll focus on widgets using AJAX technologies. Widgets use HTML, JavaScript, and CSS and can use other technologies such as SVG. Widgets have a configuration file and are packaged in the ZIP format and have their extension renamed .wdgt.
Because a widget is a small application, it's focused on a simple task. Note that in environments like the Opera browser, widgets can call other widgets. Hence, more complex applications can be developed using the widget's philosophy. However, for now, let's look only at simple widgets. Also, note that widgets should be self-contained, i.e.. they shouldn't stop working when the widget is online.
The Opera Platform
As we have seen before, we need an AJAX framework on the device and access to device APIs to create a useful mobile application. This function is performed by the Opera platform, which acts as an SDK and a framework for AJAX mobile applications.
The Opera Platform is comprised of:
- An Opera Web browser running in full-screen mode
- An AJAX framework for running multiple widgets/applications
- Access the DOM and the phone's native functionality through an abstraction layer (APIs)
At the moment, Opera widgets can be published on the My Opera community or you could deploy the widget in your own environment. Although widgets are similar to Web pages, there are some differences.
Note these apply to the Opera environment but can illustrate general widget development:
- The widget lives outside the Web browser directly on the user's desktop without any of the regular user interface elements such as title bars.
- The widget's security restrictions are different from regular Web pages, enabling you to create a widget that will simultaneously interface with different Web Services living on different Web servers.
- Widgets have a widget object available from JavaScript that lets an author access widget-specific functionality.
- A widget has access to permanent storage for its settings and downloaded data. This mechanism is similar to cookies, but the storage capacity is larger than for cookies and does not automatically expire after a given time.
- Widgets typically have several views available in the same document. One of these views is what a widget user typically sees when using the widget, and another one is where you provide the user with configuration options. Switching between these views is done by performing transitions on the views using regular JavaScript+CSS methods.
- By default a widget is draggable so the user can move it around on the screen simply by clicking and dragging the widget. If this behavior isn't desired for parts of a widget, the user will have to specify control regions where the widget doesn't respond to dragging.
This content is reprinted from Real-World AJAX: Secrets of the Masters published by SYS-CON Books. To order the entire book now along with companion DVDs, click here to order.
Published April 6, 2007 Reads 6,190
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Ajit Jaokar
Ajit Jaokar is the author of the book 'Mobile Web 2.0' and is also a member of the Web2.0 workgroup. Currently, he plays an advisory role to a number of mobile start-ups in the UK and Scandinavia. He also works with the government and trade missions of a number of countries including South Korea and Ireland. He is a regular speaker at SYS-CON events including AJAXWorld Conference & Expo.
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Windows 7 – Microsoft’s First Step to the Cloud
- Ulitzer Provides a Powerful Social Journalism Platform
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Open Source Mobile Cloud Sync and Push Email
- Practical Approaches for Optimizing Website Performance
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- US Post Office Hops a Ride on NetSuite’s Cloud
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- WPF Controls by DevExpress
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Building a Drag-and-Drop Shopping Cart with AJAX
- What Is AJAX?
- Google Maps! AJAX-Style Web Development Using ASP.NET
- Flashback to January 2006: Exclusive SYS-CON.TV Interviews on "OpenAjax Alliance" Announcement
- AJAXWorld Conference & Expo to Take Place October 2-4, 2006, at the Santa Clara Convention Center, California
- AJAX Sponsor Webcasts Are Now Available at AJAXWorld Website
- How and Why AJAX, Not Java, Became the Favored Technology for Rich Internet Applications
- "Real-World AJAX" One-Day Seminar Arrives in Silicon Valley
- AJAXWorld University Announces AJAX Developer Bootcamp
- AJAX Support In JadeLiquid WebRenderer v3.1
- Where Are RIA Technologies Headed in 2008?
- Struts Validations Framework Using AJAX
































