Page 1 of 1

Relfection in Javascript?

Posted: Tue Sep 26, 2006 12:29 am
by daedalus__
Is there a way I could get an object to return some data about itself to me? That would be reflection, correct?

Would I be able to use the prototype thing for osmething like this?

I am passing an object to a method in another object, I need to know at least the name of the object being passed.

If possible I would like to find out what methods that object has.

I am trying to use an observer-like thing for this thing I am doing.

I know that I am not being clear enough about this. (intoxicated, lolo. first time i've felt like working in a while)

So, have some code:

Code: Select all

Request=
{
	http : false,
	xmldom : false,
	init : function()
	{
		this.CreateXmlHttpRequestObject();
		if (this.http == false)
		{
			alert('Your browser does not support AJAX functionality.\nYou are now being redirected to the non-AJAX version of the website.')
			window.location = window.location + "no_ajax/";                  (lols?)
		}
	},
	CreateXmlHttpRequestObject : function()
	{	This makes the xmlrequest thing on the this.http variable	},
	CreateXmlDomObject : function ()
	{						},
	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 ()
	{
		alert('This is a default behavior, there was an error.\n'+this.http.responseText);
	}
}

Code: Select all

var Weblog=
{
	url : 'weblog.php',
	method : 'GET', 
	frame : 'contentFrame',
	requestRef : null,
	init : function (requestObj, frame)
	{
		if (frame)
		{
			this.frame = frame;
		}
		if (typeof requestObj == "object")
		{
			this.requestRef = requestObj;
		}
	},
	GetWeblog : function ()
	{
		this.requestRef.MakeRequest(method, url+'?a=get&v=all', this);		// Here's the line it stops on
															       // It says the method doesnt exist 
	},
	HandleResponse : function (responseText, ResponseXml)
	{	This is the DOM's new girlfriend	}
}
I woudl like to check what those objects are when they are passed and when it is called in the GetWeblog() function.

Possible?

Posted: Tue Sep 26, 2006 2:11 am
by daedalus__
This is messed up!

Code: Select all

	GetWeblog : function ()
	{
		alert( ( typeof this.requestRef.MakeRequest == 'function' ) ? "requestRef.MakeRequest is a function" : "requestRef.MakeRequest is not a function" );
		this.requestRef.MakeRequest(method, url+'?a=get&v=all', this);
	},
When I call this function, the alert says that this.requestRef.MakeRequest is a function, but when I try to call it the method doesn't exist!

??

Posted: Tue Sep 26, 2006 3:24 am
by Weirdan
that this.requestRef.MakeRequest is a function, but when I try to call it the method doesn't exist!
Daedalus, 'method' is a variable name! That variable is not defined in your constructor (as well as 'url').

Posted: Tue Sep 26, 2006 4:09 am
by Chris Corbyn
passedObj.constructor

Is this for type checking?

Code: Select all

this.loadObject = function(someObj)
{
    if (!(someObj instanceof WantedClass)) throw new Error('loadObject() expects parameter 1 to be of type WantedClass');
}
Actual class name:

Code: Select all

this.loadObject = function(someObj)
{
    var isClass;
    if (matches = someObj.constructor.match(/^function\s*(\w+)/i))
    {
        isClass = matches[1];
    }
    else isClass = '{anonymous}';
    
    alert(isClass); //WantedClass ?
}

Posted: Tue Sep 26, 2006 1:38 pm
by daedalus__
Now it tells me this:

Error: uncaught exception: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXMLHttpRequest.open]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: http://localhost/camicus/inc/js/request.ajax.js :: anonymous :: line 78" data: no]

I'm gonna keep messing with it, I think I almost have it working :)

Posted: Wed Sep 27, 2006 1:45 pm
by daedalus__
I've got the script working, for the most part, except I can't get a response from the server.

All you have to do is echo some plain text and it gets returned to the server, right?

I put it online if anyone would care to examine it.

http://www.camicus.net

http://www.camicus.net/weblog.php?a=get&v=all

Posted: Wed Sep 27, 2006 10:09 pm
by Weirdan

Code: Select all

		if (typeof obj == 'object')
		{
			if (this.http.responseText != '')
			{
				this.http.onreadystatechange = obj.HandleResponse(this.http.responseText);
			}
			else if (this.http.responseXml != undefined)
			{
				this.http.onreadystatechange = obj.HandleResponse(this.http.responseXml);
			}
			else
			{
				this.http.onreadystatechange = this.HandleResponse('No response from server.');
			}
		}
		else
		{
			this.http.onreadystatechange = this.HandleResponse('No module referenced!');
		}
		this.http.send(null);
	},
Here's the problem. You're checking responseText/responseXML before you have sent the request. Obviously they would be empty.

Posted: Thu Sep 28, 2006 1:29 am
by daedalus__
Fixed that already :(

I've narrowed it down to one thing though. I feel retarded. :P

Code: Select all

	HandleResponse : function (response)
	{
		if (this.requestRef.http.readyState == 4)
		{
			alert("Weblog object is responding... readyState == 4\n"
			+ "Status: " + this.requestRef.http.status);
		}
		else
		{
			alert(this.requestRef.http.readyState);
		}
	}
This produces a single alert with the number 1.

I tried to use a while loop to make it keep going but that just made my browser unhappy.