0

我无法在对象中创建“实例”变量,或者在将对象的函数作为事件处理程序调用时在对象中引用“实例”变量。下面是我正在使用的代码。如果重要的话,它(显然)使用dojo。我遇到的问题是否特定于我的案例?特定于道场?还是我以完全不正确的方式解决问题?

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");
  }
});
4

1 回答 1

0

你需要使用dojo.hitch. 这将为函数设置一个范围。

dojo.connect(this.myLocalMap, "onMouseDown", 
    dojo.hitch(this, this.DynDraw_Map_OnMouseDown));

http://dojotoolkit.org/reference-guide/1.7/dojo/hitch.html

于 2013-01-16T14:56:07.677 回答