Recent Weblogs

Links I like


Ajax.Application.Base is a javascript class for which to extend your classes from. It handles the basics of sending ajax requests. I noticed pretty early on when working with prototype that my onFailure and onException handlers were pretty similar throughout all calls. The only thing that changed was the onComplete. I have been working a lot with Firebug so it has some friendly code to just log errors. If you're working with prototype it'd be a great idea to get firebug

Ajax.Application.Base can be thought of as an abstract class, we can't define that in the syntax but it has no initialize function(constructor).

var myApp = new Ajax.Application.Base();// won't work.
This class is an abstract from which you extend from
Object.extend(Object.extend(myClass.prototype, Ajax.Application.Base.prototype), { initialize : function(){ //oh boy, i am concrete } });
Now my Class has inherited all functions of Ajax.Application.Base and can now send its own requests from an internal method, easily accessed via this.sendRequest().

Send Request

sendRequest receives two parameters, the first being the dto or parameters of the Ajax.Request that is going to be created. This can be a hash object { hi : "there" } or it can be a query string "hi=there&you=gohome". The second is the call back function which should be executed when the request has finished.

The URL Property

Ajax.Application.Base utilizes a url property which must be set at some point before a request is made, this is the url that will be executed for all requests, all subclasses must inherit a url property for the Base class functions to work.

The three functions of an Ajax request

  • onComplete
  • onFailure
  • onException
The Ajax.Application.Base class has handlers for each of these three functions. The advantages of doing it this way is that all of your application's requests go through a single referenced function (Not a bottle neck) but as a gateway which all data must pass through, a lot of times I've been able to easily handle invalid character issues with the server scripts by running formatting functions on the data before it is sent. To run extra logic in the responder function you only need to override the receiveRequest function.

 * @contributor Matthew Foster
 * @date 		February 4th 2007
 * @purpose 	This class should be considered an "abstract" it is never intended to be a concrete class, notice there is no initialize function.
 *				This class is intended to be extended from, allowing an overwritable yet base functions for processing ajax requests.  The proposed advantage to this structure
 *				Is that during debugging all of your requests are being processed in the same area, thus allowing an easier target for reviewing ajax data.  As well as handling
 *				more of what I believe to be the monotoneous routines involved in sending ajax requests.
Ajax.Application = {}

Ajax.Application.Base = Class.create(); 
                    sendRequest : function(dto, cb){
                        var p = "";
                        if(typeof dto == "object")
                            p = $H(dto).toQueryString();
                        else if(typeof dto == "string")
                            p = dto;
                            throw { message : "object sent to sendRequest was invalid type.  Type = " +typeof dto };
                        new Ajax.Request(this.url,
                                                parameters   : p,
                                                onComplete   : this.receiveRequest.bind(this, cb),
                                                onFailure    : this.ajaxFailure.bind(this),
                                                onException  : this.ajaxException.bind(this)
                    receiveRequest : function(cb, eAja){
                    ajaxFailure : function(){
                        if(typeof console == "object")
                            console.log("Ajax Request failed args = %o ", arguments);
                            alert("Ajax request has failed. Application integrity has been compromised.");
                    ajaxException : function(){
                        if(typeof console == "object")
                            console.log("ajax exception occured args = %o", arguments);


Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mfoster'@'' (using password: YES) in /home/content/m/a/t/mattfoster01/html/includes/php_common/lib/classes/abstract.mySQL.php on line 15

Fatal error: Uncaught exception 'Exception' with message 'Failed to connect to mySQL server in method : mySQLBase::connect' in /home/content/m/a/t/mattfoster01/html/includes/php_common/lib/classes/abstract.mySQL.php:16 Stack trace: #0 /home/content/m/a/t/mattfoster01/html/includes/template/comment.php(29): mySQLBase->connect('', 'mfoster', 'havef8th') #1 /home/content/m/a/t/mattfoster01/html/blog/2007/04/ajax-application.php(136): include_once('/home/content/m...') #2 {main} thrown in /home/content/m/a/t/mattfoster01/html/includes/php_common/lib/classes/abstract.mySQL.php on line 16