SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot sonic_2k_uk's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Method Properties...

    Hi,

    I have been working on an Ajax class, but have encountered a problem in IE which i cant seem to work around. In IE i get the following error, referring to the requestChange method.

    "this.objParent.objRequest" is null or not an object

    As the requestChange method is called as the onreadystatechange function, ive had to assign a variable referencing the parent Ajax object to the requestChange method, which is done in the Ajax construct method.

    This allows me to reference the Ajax Request Object from the requestChange function.... in firefox at least.

    The code that i currently have is as follows:

    Code:
    var Ajax = Class.extend (
    {
    	
    	
    	/*
    	* Ajax.construct Function
    	* Constructor function
    	*/
    	
    	construct: function ()
    	{
    		
    		// Set Variables
    		
    		this.strURL		= null;
    		this.strQuery	= null;
    		this.strReturn	= null;
    		this.objRequest	= false;
    		this.objParent	= false;
    		
    		// Set the request change parent
    		
    		this.requestChange.objParent = this;
    		
    		// Set HTTP Request object
    		
    		this.setRequest ();
    		
    	},
    	
    	
    	/*
    	* Ajax.setRequest Function
    	* Load the correct request object
    	*/
    	
    	setRequest: function ()
    	{
    		
    		// Figure out which request to load
    		
    		if (window.XMLHttpRequest)
    		{
    			
    			// Mozilla or Safari
    			
    			this.objRequest = new XMLHttpRequest ();
    			
    		}
    		else if (window.ActiveXObject)
    		{
    			
    			// IE
    			
    			this.objRequest = new ActiveXObject ("Microsoft.XMLHTTP");
    			
    		}
    		
    	},
    	
    	
    	/*
    	* Ajax.setReturn Function
    	* Set the return function to be called
    	* @param mixed mixReturn Return function
    	*/
    	
    	setReturn: function (mixReturn)
    	{
    		
    		// If the type is an array
    		
    		if (mixReturn.constructor.toString().indexOf("Array") != -1)
    		{
    			
    			this.objParent	= mixReturn[1];
    			this.strReturn	= mixReturn[0];
    			
    		}
    		else
    		{
    			
    			// Otherwise just set it
    			
    			this.strReturn = mixReturn;
    			
    		}
    		
    	},
    	
    	
    	/*
    	* Ajax.setQuery Function
    	* Set the query string to be passed
    	* @param string strQuery Query string
    	*/
    	
    	setQuery: function (strQuery)
    	{
    		
    		// Set the query string
    		
    		this.strQuery = strQuery;
    		
    	},
    	
    	
    	/*
    	* Ajax.setURL Function
    	* Set the requested URL
    	* @param string strURL Requested URL
    	*/
    	
    	setURL: function (strURL)
    	{
    		
    		// Set the URL
    		
    		this.strURL = strURL;
    		
    	},
    	
    	
    	/*
    	* Ajax.Get Function
    	* Send a get ajax request
    	*/
    	
    	Get: function ()
    	{
    		
    		// Set the state change function for the request object
    		
    		this.objRequest.onreadystatechange	= this.requestChange;
    		
    		// Open request
    		
    		this.objRequest.open ('GET', this.strURL, true);
    		
    		// Send the request
    		
    		this.objRequest.send (this.strQuery);
    		
    	},
    	
    	
    	/*
    	* Ajax.Post Function
    	* Send a post ajax request
    	*/
    	
    	Post: function ()
    	{
    		
    		// Set the state change function for the request object
    		
    		this.objRequest.onreadystatechange	= this.requestChange;
    		
    		// Open request
    		
    		this.objRequest.open ('POST', this.strURL, true);
    		
    		// Set request header
    		
    		this.objRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    		
    		// Send the request
    		
    		this.objRequest.send (this.strQuery);
    		
    	},
    	
    	
    	/*
    	* Ajax.requestChange Function
    	* Ran when the ajax object state changes
    	* readyState Codes:
    	* 0 = uninitialized
    	* 1 = loading
    	* 2 = loaded
    	* 3 = interactive
    	* 4 = complete
    	*/
    	
    	requestChange: function ()
    	{
    		
    		// If the request is complete
    		
    		if (this.objParent.objRequest.readyState == 4)
    		{
    			
    			// Run the return function and pass the returned text
    			
    			if (this.objParent.objParent)
    			{
    				
    				eval ('this.objParent.objParent.' + this.objParent.strReturn + '.call(this.objParent.objParent, this.objParent.objRequest.responseText);');
    				
    			}
    			else
    			{
    				
    				eval (this.objParent.strReturn + '(this.objParent.objRequest.responseText);');
    				
    			}
    			
    		}
    		
    	}
    	
    	
    	
    });
    
    // End Class
    What im after is a way to set the objParent property of the requestChange method to the Ajax object, as i have done for firefox, in IE.

    Cheers for any help...

    Andy Burton

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you use .call to pass the object reference instead?

  3. #3
    SitePoint Zealot sonic_2k_uk's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, and thanks for your reply.

    Could you elaborate a little? I am unsure how you would go about implementing the call.

    Cheers,

    Andy Burton

  4. #4
    SitePoint Zealot sonic_2k_uk's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, i found a site which helped me solve my problem: http://weblogs.macromedia.com/mesh/a...ulating_a.html

    If anyone would like a copy of the finished code please send me a message...

    Cheers.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •