dcsimg
Ajax Object encapsulation
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   tom_parker
Posted On:   Monday, May 22, 2006 07:01 PM

Hi All, I am trying to set up multiple request and responses which are tied to specific objects. My thought is to create objects that contain a response handler that is assigned durring the request. Most of it works until I get to the callback handler in the object and it just doesn't seem that my object handler is part of the object any more. I have been having a terrible time trying to find Object information on Javscript that talks about anything close. Examples would be function myObj{ var someAttribute = 'somevale'; var callBack = myHandler; function myHandler(req){ //extract data from request newData = returnedData; this.someAttribute = newData    More>>

Hi All,


I am trying to set up multiple request and responses which are tied to specific objects. My thought is to create objects that contain a response handler that is assigned durring the request. Most of it works until I get to the callback handler in the object and it just doesn't seem that my object handler is part of the object any more. I have been having a terrible time trying to find Object information on Javscript that talks about anything close.


Examples would be

			
function myObj{
var someAttribute = 'somevale';
var callBack = myHandler;

function myHandler(req){
//extract data from request
newData = returnedData;
this.someAttribute = newData
alert(this.someAttribute); //Expecting new data here, but fails.
}
}

//elsewhere in code
function makeRequest(myObj){
alert(myObj.someAttribute); //this seems to work
xhr.onreadystatechange = myObj.callback; //This is called but doesn't seem to be tied to myObj any more
}


Not sure if this makes sense. Any Ideas?

Tom    <<Less

Re: Ajax Object encapsulation

Posted By:   tom_parker  
Posted On:   Wednesday, May 24, 2006 06:16 PM

Follow up

One solution..... closures, and a bit of a hack. Simply, I created an object that contains it's own handler function. That function returns an anonymous function to the ajax response callback method. That function/method makes a call to another method passing the reference to the original object. Since the XMLHTTPRequest Obj is making the call, my handler cannot use the "this" key word, so I create my own. In my closure that returns a method handler, I store a reference of this to some other variable like "me". Then I reference the me object instead of this in my handler and all is fine.

I found the basic idea here:
http://blog.livollmers.net/?p=17


//the object I'll be using
function MyObj(){
MyObj.prototype.responseHandler = function(){
var someProperty = 'someValue';
var me = this //store this obj refernece for future use
return function(){me.myObjectsHandler()}//the closure returned to Ajax which makes a call to the objects handler. Pretty nifty trick, though not as nice as I would like.

}

MyObj.prototype.myObjectHandler = function(){
//execute my code here.
}



There are some variations. I actually ended up using a function call for the closure instead of an method call on an object. someGlobalFunction(me). Then I referenced me from inside the function (ex me.someProperty = 'newValue';).

Again, not sure if all this makes sense or will work as is as I just pounded it out. Basic idea, use a closure, store the references somewhere, the make a call from the closure to your real method using the stored reference.

This isn't cut and paste stuff needless to say.
Tom
About | Sitemap | Contact