Posted By:
tom_parker
Posted On:
Wednesday, May 24, 2006 06:16 PM
Follow upOne 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