Rounding out chapter 3
Looking again at the UserProfile object, almost every aspect of it, from the events object to the loadValues method, can easily get pulled out into a parent object that other data models can extend. The same also goes for the ProfileController object. Using inheritence in this manner not only prevents writing redundant code, it also vastly reduces the amount of JavaScript the browsers must download in order to run the web application in the first place. Data objects then only need to contain the code specific to that data, such as custom validation or authorization checks, rather than requiring that each data object also manage its own communications with the server.
Coupling the power of inheritence with the MVC pattern, an application gains a wealth of power in the form of quickly developed modules of functionality, supporting the overall application structure. Data filtering, cleaning, and management get supported by default, along with automatic updating of the server with the appropriate data no matter how convoluted an interface the user uses to interact with it.
These methodologies, combined with event-driven application development, provide a well-rounded base for many Ajax-driven applications. By using events to trigger actions throughout the application, objects stay abstracted enough for reuse in multiple interfaces, without the need for custom code to "hook in" the objects needing to interact with it.
As with all patterns, they should get regarded as tools to use for their suited purpose rather than rules to follow. The MVC pattern can add unnecessary layers to an otherwise small and simple interface. Event-driven architectures can add meaningless abstractions and hoops to jump through when dealing with large data sets or "streaming" results. In short: design the application for the requirements at hand. Doing otherwise will add complications and make the code much more difficult to maintain and debug.
Labels: advanced ajax, excerpts, javascript

2 Comments:
When will you post your source code? Right now, I get an error with the ProfileController. addElementListener is not defined.
I've just posted the MVC example at http://advancedajax.frozen-o.com/ - had to make a couple of little changes for it to completely work (writing examples for the book means generally not having to finish the code :-) ), but it sounds like the ProfileController's extension of the EventDispatcher didn't really happen. Do you have the library files (this example uses main.lib.js, ajax.lib.js, and effects.lib.js) included in the HTML head?
Post a Comment
Links to this post:
Create a Link
<< Home