Dojo AMD模块更改了参考;这";

Dojo AMD模块更改了参考;这";,dojo,amd,Dojo,Amd,我真的很困惑,我在尝试学习AMD风格的Dojo时看到了一些行为。当我实例化模块/对象时,“this”指的是构造函数中的对象。我调用了一个内部函数,该内部函数中的“this”指的是窗口对象。因此,当我访问this.attachMapEventHandlers时,我得到一个“Object[Object global]没有方法'attachMapEventHandlers'”错误。我做错了什么?更新:我找到了lang.hitch,这似乎表明异步特性让我感到困惑,但我对如何实现解决方案感到困惑 inde

我真的很困惑,我在尝试学习AMD风格的Dojo时看到了一些行为。当我实例化模块/对象时,“this”指的是构造函数中的对象。我调用了一个内部函数,该内部函数中的“this”指的是窗口对象。因此,当我访问this.attachMapEventHandlers时,我得到一个“Object[Object global]没有方法'attachMapEventHandlers'”错误。我做错了什么?更新:我找到了lang.hitch,这似乎表明异步特性让我感到困惑,但我对如何实现解决方案感到困惑

index.html中的我的脚本:

require(["javascript/layout", "dijit/layout/ContentPane", "dijit/layout/BorderContainer", "dijit/layout/AccordionContainer", 
        "dojo/dom", "dojo/dom-attr", "dijit/Toolbar", "dijit/form/Button", "dijit/Dialog","dijit/ProgressBar", "dojo/domReady!"],
        function (layout, dom, domAttr) {
            mapControl = new layout();
layout.js:

define(["dojo/_base/declare"], function(declare) {
return declare(null, {
    action:"pan",
    activeMeasureTool:"",
    aerialLayer:"",
    legendLayers:"",
    loadedServices:"",
    popup:"",
    resizeId:0,
    constructor: function() {
        this.init();
    },
    init: function() {
        require(["esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"], 
            function(Map, config, SpatialReference, Extent) {
            //custom map requires a proxy to function properly.
            esri.config.defaults.io.proxyUrl = "../sdc_devdata/proxy.php";

            var spatRef = new SpatialReference(2276);

            var startExtent = new Extent(2481416.32087491, 6963246.42495962, 2501196.36936991, 6980267.92469462, spatRef);
            var appFullExtent = new Extent(2396699.46935379, 6872369.60195443, 2607745.94404633, 7107335.22319087, spatRef);

            map = new Map("map", {extent: startExtent, isZoomSlider:true, logo:false, sliderStyle:"large"});
            this.attachMapEventHandlers();
            this.createLayers();
            this.handleLayerVisibilityChange();
        });
    },

你可以把“this”放在另一个变量中,比如这个,然后在你的内部函数中使用这个,比如

init: function() {
         var _this= this;
        require(["esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"], 
            function(Map, config, SpatialReference, Extent) {
            ...
            _this.attachMapEventHandlers();
            _this.createLayers();
            _this.handleLayerVisibilityChange(); 

你可以把“this”放在另一个变量中,比如这个,然后在你的内部函数中使用这个,比如

init: function() {
         var _this= this;
        require(["esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"], 
            function(Map, config, SpatialReference, Extent) {
            ...
            _this.attachMapEventHandlers();
            _this.createLayers();
            _this.handleLayerVisibilityChange(); 

你可以做一些事情来解决这个问题

首先,您可以将所需的依赖项添加到
define
数组中,这样就不需要在类的构造函数中执行异步require

这看起来像:

define(["dojo/_base/declare", "esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"], function (declare, Map, config, SpatialReference, Extent) {
  return declare(null, {
    action: "pan",
    activeMeasureTool: "",
    aerialLayer: "",
    legendLayers: "",
    loadedServices: "",
    popup: "",
    resizeId: 0,
    constructor: function () {
      this.init();
    },
    init: function () {
      //custom map requires a proxy to function properly.
      esri.config.defaults.io.proxyUrl = "../sdc_devdata/proxy.php";

      var spatRef = new SpatialReference(2276);

      var startExtent = new Extent(2481416.32087491, 6963246.42495962, 2501196.36936991, 6980267.92469462, spatRef);
      var appFullExtent = new Extent(2396699.46935379, 6872369.60195443, 2607745.94404633, 7107335.22319087, spatRef);

      map = new Map("map", {
        extent: startExtent,
        isZoomSlider: true,
        logo: false,
        sliderStyle: "large"
      });
      this.attachMapEventHandlers();
      this.createLayers();
      this.handleLayerVisibilityChange();
    }
  });
});
或者,您可以在执行require时将
this
的当前范围保存到闭包中的某个内容

init: function () {
  var that = this;
  require(["esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"],
    function (Map, config, SpatialReference, Extent) {
      //custom map requires a proxy to function properly.
      esri.config.defaults.io.proxyUrl = "../sdc_devdata/proxy.php";

      var spatRef = new SpatialReference(2276);

      var startExtent = new Extent(2481416.32087491, 6963246.42495962, 2501196.36936991, 6980267.92469462, spatRef);
      var appFullExtent = new Extent(2396699.46935379, 6872369.60195443, 2607745.94404633, 7107335.22319087, spatRef);

      map = new Map("map", {
        extent: startExtent,
        isZoomSlider: true,
        logo: false,
        sliderStyle: "large"
      });
      that.attachMapEventHandlers();
      that.createLayers();
      that.handleLayerVisibilityChange();
    });
},
编辑:第三个选项是使用lang.hitch,它允许您在回调函数中指定
this
的范围。要使用它,您需要将
dojo/_base/lang
添加到define()依赖项列表中,并将require回调包装在
lang.hitch中(这个函数是(){})


我强烈建议使用第一个选项,因为它与AMD的整个使用是一致的(在执行之前声明模块需要什么依赖项,而不是动态加载它)。

要解决这个问题,您可以做几件事

首先,您可以将所需的依赖项添加到
define
数组中,这样就不需要在类的构造函数中执行异步require

这看起来像:

define(["dojo/_base/declare", "esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"], function (declare, Map, config, SpatialReference, Extent) {
  return declare(null, {
    action: "pan",
    activeMeasureTool: "",
    aerialLayer: "",
    legendLayers: "",
    loadedServices: "",
    popup: "",
    resizeId: 0,
    constructor: function () {
      this.init();
    },
    init: function () {
      //custom map requires a proxy to function properly.
      esri.config.defaults.io.proxyUrl = "../sdc_devdata/proxy.php";

      var spatRef = new SpatialReference(2276);

      var startExtent = new Extent(2481416.32087491, 6963246.42495962, 2501196.36936991, 6980267.92469462, spatRef);
      var appFullExtent = new Extent(2396699.46935379, 6872369.60195443, 2607745.94404633, 7107335.22319087, spatRef);

      map = new Map("map", {
        extent: startExtent,
        isZoomSlider: true,
        logo: false,
        sliderStyle: "large"
      });
      this.attachMapEventHandlers();
      this.createLayers();
      this.handleLayerVisibilityChange();
    }
  });
});
或者,您可以在执行require时将
this
的当前范围保存到闭包中的某个内容

init: function () {
  var that = this;
  require(["esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"],
    function (Map, config, SpatialReference, Extent) {
      //custom map requires a proxy to function properly.
      esri.config.defaults.io.proxyUrl = "../sdc_devdata/proxy.php";

      var spatRef = new SpatialReference(2276);

      var startExtent = new Extent(2481416.32087491, 6963246.42495962, 2501196.36936991, 6980267.92469462, spatRef);
      var appFullExtent = new Extent(2396699.46935379, 6872369.60195443, 2607745.94404633, 7107335.22319087, spatRef);

      map = new Map("map", {
        extent: startExtent,
        isZoomSlider: true,
        logo: false,
        sliderStyle: "large"
      });
      that.attachMapEventHandlers();
      that.createLayers();
      that.handleLayerVisibilityChange();
    });
},
编辑:第三个选项是使用lang.hitch,它允许您在回调函数中指定
this
的范围。要使用它,您需要将
dojo/_base/lang
添加到define()依赖项列表中,并将require回调包装在
lang.hitch中(这个函数是(){})


我强烈建议使用第一个选项,因为它与AMD的整个使用是一致的(在执行之前声明模块需要哪些依赖项,而不是动态加载)。

我感谢您的回答。我接受了第一个我感谢你的回答。我接受了第一个