I am having trouble creating an “instance” variable in an object, or referencing an “instance” variable in an object when an object’s function is called as an event handler. below is the code I am working with. It is (obviously) using dojo if that matters. Is the issue I am encountering specific to my case? Specific to dojo? Or am I approaching the problem in an entirely improper fashion?
dojo.declare("DynDraw", esri.toolbars.Draw, {
constructor: function () {
this.myLocalMap = arguments[0];
}
, myLocalMap: null
, subscribed: false
, activate: function (geometryType, options) {
this.inherited(arguments);
dojo.connect(this.myLocalMap, "onMouseDown", this.DynDraw_Map_OnMouseDown);
}
, DynDraw_Map_OnMouseDown: function (event) {
//when called as an event handler (assigned above) “this.subscribed” is undefined
//”this” seems to be “myLocalMap”, not the “DynDraw” object
//when called as a “function” “this.subscribed” has a
if (this.subscribed == false) {
dojo.connect(this.myLocalMap, "onMouseMove", this.DynDraw_Map_OnMouseMove);
this.subscribed = true;
}
}
, DynDraw_Map_OnMouseMove: function (event) {
console.log("DynDraw_Map_OnMouseMove");
}
});
You need to use
dojo.hitch. This will hitch a scope to the function.http://dojotoolkit.org/reference-guide/1.7/dojo/hitch.html