Wednesday, April 16, 2008

Easy AJAX Steps for Success

Here are 10 helpful hints if you are considering using AJAX in an upcoming project:
  1. You don't need an AJAX library
  2. Download Aptana. Make it your default .js editor inside Visual Studio.
  3. Place all Javascript in external files (like codebehind)
  4. Instead of making invoking requests directly from your pages, build a class library that does the dirty work.
    hint: once you have these base libraries, add the files the code assist window to get auto-complete inside Aptana. Aptana lets you add documentation comments as well as parameter and return type information. Use it!
  5. You don't need an AJAX library.
  6. You can make classes in Javascript too.
  7. You don't have to define seperate functions for async callbacks from webservices. Use anonymous methods to inline your code in the calling method.
  8. Consider providing ways to extend standard HTML elements rather that trying to make up all sorts of funky controls. Controls have a place, but consider the common example of a grid. A grid is really just a table. Don't create a grid class, provide a way extend the table and add column sorts, row hover handling, row selection, etc. to it. Then, you can use standard HTML layouts and design and simply call a function or two to inject the features you want.
  9. Make sure to secure your AJAX server side.
  10. You don't need an AJAX library.

Why do I point out over and over that you don't need an AJAX library? Because all the AJAX hype out there makes the problems out to be a lot more difficult than they are. I would recommend one little script to abstract away SOAP or JSON requests, since you probably don't want to build that manually, but you don't really need much else to get started building AJAX into your applications. The most important thing to do is get a solid understanding of Javascript and CSS and make sure that you use standard DOM properties as much as possible. A great editor like Aptana can also be an extremely valuable tool. This isn't to say that using an AJAX library is a bad thing, just that you can get by fine without one if you would rather not add the extra dependancies and complexity to your app. The browser differences are not nearly as bad as the mobs might have you believe.

Also keep in mind that you don't have to make every element clickable and every node draggable. Web apps are not windows apps. You don't need one base page that everything is dynamically added to and removed from. On the contrary, it is nice to have back and forward buttons that actually do something. A lot of improvement can be made by simply adding a few AJAX callbacks to eliminate those annoying pages you used to have to make with 2 lines of text and a button.

No comments: