• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Object Oriented Javascript Problem

puffpio

Golden Member
is there anyway to set the callback to the particular instance of the object that intiiated it?

here's my sample code class:

-------------

function MyClass() {}

MyClass.prototype.GetStuff = function()
{
var myAJAX = new AJAX();
myAJAX.sendBegin("somewebservice", "HandleGetStuff"); // this needs to be changed somehow
}

MyClass.prototype.HandleGetStuff = function(incoming)
{
// do stuff with incoming
}

--------------

later on in the code:

---------------

var myClass1 = new MyClass();
var myClass2 = new MyClass();

myClass1.GetStuff(); // should be handled by it's own HandleGetStuff
myClass2.GetStuff(); // ditto
 
You'll need to learn more about javascript scope rules. Try this:
MyClass.prototype.GetStuff = function()
{
var myAJAX = new AJAX();
var instance = this;
myAJAX.sendBegin("somewebservice", function (arg1, arg2, arg3) {
instance.HandleGetStuff(arg1, arg2, arg3);
});
}

The cleaner way to accomplish this is to add a "bind" method to the Function prototype (or the entire prototypejs library).
Take a look here: http://www.prototypejs.org/api/function/bind
Then change your class to:
MyClass.prototype.GetStuff = function()
{
var myAJAX = new AJAX();
myAJAX.sendBegin("somewebservice", this.HandleGetStuff.bind(this));
}
 
Back
Top