Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good book or resource for writing large ajax applications? [closed]

I am very experienced in engineering large-scale systems, but I am still relatively new to ajax-based design. I know how to use the apis, and I am fairly comfortable using jquery and javascript as a whole, but I often find myself thinking way too hard about the overall architecture.

Right now, my current application just has javascript files sprinkled all over the place, all in a /js directory. Most of them use jQuery, but some use YUI or a combination between the two because the features weren't available in jQuery.

Some of my REST server methods accept normal GET methods with request parameters, but others needed much more complex POSTs to handle the incoming data (lists of lists of objects). The handling of all of my ajax stuff is a mix and mash of different methods as a result of the complexity of the data I'm dealing with.

What I'd really like is to read about how to design an ajax-based system that is very clean and elegant architecturally, and is consistent from the simplest to the most complex of cases. Does such a resource exist?

Also suggestions on naming conventions of javascript files and conventions for ajax endpoint directory/method names?

Also how to do with entering form data? Should you use get or post to do this?

Also about validation of form data when all the constraints are already on the server? How to make this very trivial to do so you're not doing it for each form?

What are the best ways to generate new page content when people click things and settings this up so that it's easy to do over and over.

How to deal with application-specific javascript files depending on each other and managing this nicely.

I am also using Spring and Spring-MVC, but I don't expect this to make much difference. My questions are purely browser related.

like image 364
egervari Avatar asked Dec 09 '10 21:12

egervari


2 Answers

There's a TL;DR summary at the end.

I can't really point you to a good resource for this as I haven't found one myself. However, all is not lost. You already have experience in developing large-scale applications and taking this knowledge into the browser-space doesn't require a lot of re-thinking.

First of all, unless your application is really trivial, I wouldn't start refactoring the entire codebase straight away because there are bound to be endless cases you haven't thought of yet.

Design the core architecture of the system you want first. In your case you probably want all your AJAX requests to go through one point. Select the XHR interface from either jQuery or YUI and write a wrapper around it that takes an option hash. All the XHR calls you write for new code go through there. This allows you to switch out the framework performing the XHR calls at any time with another framework or your own.

Next up, harmonize the wire protocol. I'd recommend using JSON and POST requests (POST requests have the additional benefit for FORM submissions of not being cached). Make a list of the different types of request/responses you need. For each of these responses, make a JS object to encapsulate them. (E.g. the form submission response is returned to the caller as a FormReponse object which has accessor functions for the validation errors, etc). The JS overhead for this is totally trivial and makes it easy to change the JSON protocol itself without going through your widget code to change the access of the raw JSON.

If you're dealing with a lot of forms, make sure they all have the same structure so you can use a JS object to serialize them. Most frameworks seem to have various native functions to do this, but I'd recommend rolling your own so you don't have to deal with shortcomings.

The added business value at this point is of course zero because all you have is the start of a sane way of doing things and even more JS code to load into your app.

If you have new code to write, write it on the APIs you've just implemented. That's a good way to see if you're not doing anything really stupid. Keep the other JS as it is for now but once you have to fix a bug or add a feature there, refactor that code to use your new APIs. Over time you'll find that the important code is all running on your APIs and a lot of the other stuff will slowly become obsolete.

Don't go overboard with re-inventing the wheel, though. Keep this new structure limited to data interaction and the HTTP wire and use your primary JS framework for handling anything related to the DOM, browser quirks, etc.

Also set up a global logger object and don't use console directly. Have your logger object use console or a custom DOM logger or whatever you need in different environments. That makes it easy to build in custom log levels, log filters, etc. Obviously you have to set up your build environment to scrub that code out for production builds (you do have a build process for this, right?)

My personal favorite for relatively sane JS source-code layout and namespacing is the Dojo framework. Object definitions relative to their namespace have obvious relations to their location on disk, there's a build system in place for custom builds, third-party modules, etc. The Dojo dependency/build system depends on dojo.require and dojo.provide statements in the code. When running on source a dojo.require statement will trigger the blocking load of the resource in question. For production the build system follows these statements and inserts the resource into the final bundle at that location. The documentation is a bit sparse, but it's definitely a good start for inspiration.

The TL;DR answer is,

  • Have all XHR calls go through a single interface
  • Don't pass raw response data back to higher levels
  • Do gradual refactoring of existing code
  • Harmonize the wire protocol
  • Use the dynamic power of JS for building light-weight proxy code
  • Sane code structure and call graphs look the same in JS as they do in other languages
like image 55
Michiel Kalkman Avatar answered Oct 17 '22 22:10

Michiel Kalkman


Ajax Patterns is a pretty awesome book/site: http://ajaxpatterns.org/

Otherwise you may want to check out Advanced Ajax Architecture Best Practices

How you go about designing your site should be based on the features and size of your app. I would keep your focus there as opposed to looking for a architecture that works for all cases.

like image 26
wajiw Avatar answered Oct 17 '22 22:10

wajiw