Problem referencing objects in JS

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Problem referencing objects in JS

Post by daedalus__ »

I have some code and it looks like this:
request.js wrote:

Code: Select all

Request=
{
	http : false,
	xmldom : false,
	init : function()
	{
		/*somecode*/
	},
	CreateXmlHttpRequestObject : function()
	{
		/*somecode*/
	},
	CreateXmlDomObject : function ()
	{
		/*somecode*/
	},
	MakeRequest : function (method, url, obj)
	{
		this.http.open(type, url)
		if (typeof obj == 'object')
		{
			this.http.onstatechange = obj.HandleResponse(
										this.http.responseText, 
										this.http.ResponseXml);
		}
		else
		{
			this.http.onstatechange = this.HandleResponse();
		}
	},
	HandleResponse : function ()
	{
		/*somecode for default response handling*/
	}
}
weblog.js wrote:

Code: Select all

var Weblog=
{
	url : 'weblog.php',
	method : 'GET', 
	frame : 'contentFrame',
	request : null,
	init : function (requestObj, frame)
	{
		/*somecode*/
		if (typeof requestObj == "object")
		{
			this.request = requestObj;
		}
	},
	GetWeblog : function ()
	{
		this.request.MakeRequest(method, url+'?a=get&v=all', this);
	},
	HandleResponse : function (responseText, ResponseXml)
	{
		/*somecode*/
	}
}
page.php wrote:

Code: Select all

Weblog.init(Request, "contentFrame");
What should be happening is when I call Weblog.GetWeblog(), it calls the MakeRequest() method of my Request object.

Then the callback function that is in the Weblog object is called.

For some reason when Weblog.GetWeblog() is called, firefox says that the MakeRequest() method is undefined.

I believe that the object reference is not working.

I've looked for examples and stuff all over Google but I can't find anything.

Any help would be greatly appreciated.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Did I ask a stupid question?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Did you use firebug to step through your code?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I didn't know I could step through it with FireBug but it does give me the same error.

When I alert() the references it says that they are objects.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Step-through on its own wouldn't give you much information. Inspect this.request before the call to this.request.MakeRequest to make sure it really contains the MakeRequest method. If it doesn't - look for places where it could be removed/overrided.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

btw, shouldn't it be this.method and this.url in GetWebLog method?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Haha. That it should. Thanks.
Post Reply