OO Javascript
-
Hi, I am new to Object Oriented Javascript. I am making a javascript "class" as:
function MyClass{ this.ajaxObj = new uniAjax(); this.Start = function(url){ this.ajaxObj.request({'url': uri , 'func': this.GetResponse}, 'Resp'); } this.GetResponse = function(resp){ alert(resp); this.ProcessResponse(resp); } this.ProcessResponse = function(resp){ alert(resp); } }
This uses an AJAX framework (uniAjax) for fetching data from the server. The callback function GetResponse is able to receive the response, but the problem is that since this method is called externally by the framework, the identity of
this
is lost. Meaning that, ProcessResponse() is no longer related to the currentthis
object... and therefore it gives an error when called asthis.ProcessResponse(resp)
. Can anyone please please please please provide a solution for this. Thanks in advance. *** Who said nothing is impossible? I have been doing it for a long time *** -
Hi, I am new to Object Oriented Javascript. I am making a javascript "class" as:
function MyClass{ this.ajaxObj = new uniAjax(); this.Start = function(url){ this.ajaxObj.request({'url': uri , 'func': this.GetResponse}, 'Resp'); } this.GetResponse = function(resp){ alert(resp); this.ProcessResponse(resp); } this.ProcessResponse = function(resp){ alert(resp); } }
This uses an AJAX framework (uniAjax) for fetching data from the server. The callback function GetResponse is able to receive the response, but the problem is that since this method is called externally by the framework, the identity of
this
is lost. Meaning that, ProcessResponse() is no longer related to the currentthis
object... and therefore it gives an error when called asthis.ProcessResponse(resp)
. Can anyone please please please please provide a solution for this. Thanks in advance. *** Who said nothing is impossible? I have been doing it for a long time *** -
Make ProcessResponse a regular function instead of an anonymous function in an object, so that you can call it without a reference to the object. --- b { font-weight: normal; }
Hi Guffa, Thanks for the reply. Thats seems to be suitable solution, but my main concerns are: 1. will it be a problem if a method is accessed simultaneously from more than one place. 2. i somehow need object specific values to identify and differentiate different objects. If i put them in a global variable, then all the methods accessing this would certainly overwrite the existing value. Is there no way that i use AJAX and still retain the OO program ? Thanks. *** Who said nothing is impossible? I have been doing it for a long time ***
-
Hi Guffa, Thanks for the reply. Thats seems to be suitable solution, but my main concerns are: 1. will it be a problem if a method is accessed simultaneously from more than one place. 2. i somehow need object specific values to identify and differentiate different objects. If i put them in a global variable, then all the methods accessing this would certainly overwrite the existing value. Is there no way that i use AJAX and still retain the OO program ? Thanks. *** Who said nothing is impossible? I have been doing it for a long time ***
Make a callback function for each object, that uses the instance of the object to call the method in it. Or make a general callback function that somehow identifies what object the callback belongs to, and calls a method of that object. --- b { font-weight: normal; }