Page 1 of 1

Problem referencing objects in JS

Posted: Sun Sep 17, 2006 4:03 pm
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.

Posted: Mon Sep 18, 2006 11:32 am
by daedalus__
Did I ask a stupid question?

Posted: Mon Sep 18, 2006 12:03 pm
by Weirdan
Did you use firebug to step through your code?

Posted: Mon Sep 18, 2006 1:16 pm
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.

Posted: Mon Sep 18, 2006 2:59 pm
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.

Posted: Mon Sep 18, 2006 3:05 pm
by Weirdan
btw, shouldn't it be this.method and this.url in GetWebLog method?

Posted: Mon Sep 18, 2006 3:07 pm
by daedalus__
Haha. That it should. Thanks.