I have a javascript class declared as shown below.. My problem is the aonymous function does not see the scope of the class. I can not reference the bb_obj from within the ajax call load callback..
Is there a way to do this?
Thanks in advance..
dojo.declare("sop.quote", null,
{
bb_obj : new Object,
stage1 : function()
{
dojo.xhrPost(
{
url : 'go/module_service/transport.php',
content : this.bb_obj,
handleAs : 'xml',
load : function(xml)
{
var status = xml.getElementsByTagName("STATUS")[0].childNodes[0].nodeValue;
var message = xml.getElementsByTagName("MESSAGE")[0].childNodes[0].nodeValue;
this.bb_obj.message = message;
},
error : function()
{
}
}
}
}
thisinside a XHR callback function refers to the XHR object. The only way to refer tobb_objis by directly referring to the created object, in the same scope as the function. Because objects are passed by reference, the code below works as intended.Note to avoid confusion, I’ve declared the object using
var bb_obj_obj={}. Thebb_objproperty refers tobb_obj_obj:bb_obj_obj.messageis changedbb_objpoints tobb_obj_obj, hencebb_obj.messagerefers to the same variableCode:
An alternative method consists of saving
thisin a variable, eg.$this: