- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 ESRI Web App Builder 中使用 dojo,遇到过这样的情况:我需要运行 AJAX 调用并仍然访问基类中的变量。下面是我的代码,其中包含注释,以准确解释它成功的地方和失败的地方:
define(['dojo/_base/declare', 'jimu/BaseWidget', 'dojo/request', "esri/layers/WMSLayer", "esri/config", "dojo/domReady!"], function (declare, BaseWidget, request, WMSLayer, esriConfig) {
return declare([BaseWidget], {
baseClass: 'jimu-widget-mywidget',
// This function is called by a button press (Normally the WMSLayer variable would be set by user input)
addWms: function () {
var wmsLayer = new WMSLayer("http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer", {
format: "png",
visibleLayers: [2]
});
this.map.addLayer(wmsLayer); // this.map is inherited from BaseWidget as far as I can see. This adds a wms to my map without error
request("request.html").then(function(data){
var wmsLayer = new WMSLayer("http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer", {
format: "png",
visibleLayers: [2]
});
this.map.addLayer(wmsLayer); // This is now in another context....I get the error HERE.
// At this point map is not defined because this anonymous function is running
// in a different context. ( at least I think that's what is happening )
}, function(err){
// Hopefully there are no typos in my example XD
});
}
});
});
我的问题是 --> 如何使用“request”的回调函数访问“map”变量?
我希望能够通过调用 WMS 服务的 GetCapabilities 来运行 this.map.addLayers。请求通常会调用它,我一直走到代码的末尾,直到我无法再访问“map”变量为止。
首选 Dojo 类型的答案,但普通的旧 javaScript 也可以。请避免使用 JQuery 答案。
最佳答案
您遇到的问题是调用异步回调时丢失执行上下文的经典问题(因此 this
不再意味着您想要的)。通常有两种方法可以解决此问题。
一种方法是在外部作用域中创建一个引用this
的变量,以便内部函数可以访问它:
var self = this;
request('request.html').then(function (data) {
// ...
self.map.addLayer(wmsLayer);
});
另一种方法是使用上下文绑定(bind),通过 Function#bind
(ES5) 或 dojo/_base/lang.hitch
:
// Function#bind:
request('request.html').then(function (data) {
// ...
this.map.addLayer(wmsLayer);
}.bind(this));
// lang.hitch:
request('request.html').then(lang.hitch(this, function (data) {
// ...
this.map.addLayer(wmsLayer);
}));
将异步处理函数分解为另一个内部实例方法的绑定(bind)方法也很常见:
_addRequestedLayer: function () {
// ...
this.map.addLayer(wmsLayer);
},
addWms: function () {
// ...
// ES5:
request('request.html').then(this._addRequestedLayer.bind(this));
// ...or lang.hitch, with late binding:
request('request.html').then(lang.hitch(this, '_addRequestedLayer'));
还有一个tutorial on lang.hitch .
关于javascript - Dojo - Promise 无法访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31941404/
如何安装 DOJO 工具包并在我的网站上使用它?有教程吗? 最佳答案 是的: http://docs.dojocampus.org/quickstart/gettingstarted “Dojo 现在
我对 Dojo 的文档感到很困惑。如何在不实际使用 dojo.declare() 的情况下使用 dojo.require()?我不想使用 dojo.declare() 的原因是它将声明的类公开为全局变
我是 Dojo Toolkit 的新手,今天我遇到了这个话题 dojo.mixin()和 dojo.extend() . 我想两者都习惯于从另一个对象获取一个对象的属性。 谁能解释一下 dojo.mi
我的问题是:我们可以使用 dojo.xhrPost 发布一些 Json 数据吗?更多详情: 我一直在尝试使用 Dojo 代码将 JSON 数据发布到 RESTful 服务。似乎是的行为dojo.xhr
我有一个从XMLStore读取的DataGrid。 DataGrid中大约有2200行。每次我滚动表格时,都要花费几秒钟来显示数据。似乎一次只检索了几行(20-25)。有没有办法一次检索所有行? 最佳
在将请求发送到服务器以获取 AMD 模块之前,是否可以修改 dojo AMD 加载程序使用的请求 url?我想附加一个带有版本号的请求参数。 我们试图解决的问题是我们希望浏览器缓存我们的 javasc
当我使用自定义构建的 dojo 时,dojo.currency.format 没有使用正确的货币符号。 这是我使用的语句: dojo.currency.format(1234.567, {curren
我创建了一个非常简单的模板化小部件。像这样的东西: function (declare, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin,
如何在 dojo 数据网格中显示复选框? 最佳答案 我建议将 cellType 设置为 dojox.grid.cells.Bool,而不是格式化程序。格式化程序为您提供了很大的自由,但也有责任在之后从
收到新信息后,我无法尝试更改数据网格的结构。每次进行查询时,我都需要能够更改列数。 我用来创建网格的 javascript 代码 function setgrid(){ var gridLay
试图找出为什么我似乎无法在我的 dojo 构建中实习字符串。我的图层文件已正确创建,但与每个单独的 dijit 关联的代码未正确嵌入。 这是构建输出的一部分,说明了失败的地方: release: I
我刚刚开始使用道场,我知道 dojo.query与 $ 相同在 jQuery 中。 但我还没有弄清楚它返回什么。它是像 jQuery 一样的专门对象吗? 我正在尝试做的(没有运气)是: dojo.qu
有什么方法可以将按钮 添加到TitlePane 标题(标题栏右侧),以便我可以执行一些操作(下载、删除...) 提前致谢。 最佳答案 dijit TitlePane 标题包含以下内容 -Rule
在使用 dojo 的 gfx 渲染文本时,有没有办法告诉文本将占用多少空间,以防我需要包装它以使其适合给定的宽度? 如果您必须使它们适合宽度受限的区域,您将如何管理打印任意长度的文本? 更新 :对不起
我对 Dojo 工具包有点陌生。有些问题我想得到启发(我用谷歌搜索,但没有得到任何合适且令人满意的答案) 我已经在运行的应用程序(由另一个软件开发人员开发)中有一个 dojo.js(也许是下载的未压缩
我正在使用 dojo 柱形图。我想添加一条绘制在绘图上的自定义线(某种阈值线)。所以,假设 y 轴的范围从 0 到 5。我想要一条水平线,比方说,4.2 横跨绘图。它是一个柱形图。我希望找到一些可以帮
我在设置 dojo 时遇到问题. dojo 中定义的任何内容配置似乎使用 localhost:8080/Scripts/foo.js 正确加载小路。但是,如果我尝试在没有这个的情况下加载模块,请说:
我正在为一个项目使用 Dojo 1.9,但我不明白 dojo.provide 的正确替代方案与传统风格相比,AMD 风格。我正在阅读 this文档页面。 很明显,这就是旧语法映射到新语法的方式: 旧
我们正在使用 Dojo 1.9 运行我们的应用程序的构建,而构建本身需要过多的时间才能完成。某处沿着 10-15 分钟的路线。 我们的应用程序无论如何都不是很大。也许 150K LOC。没有什么花哨。
在我的 Dojo 构建中,我引入了一些第三方库。 当我完成构建过程时,由于 ReferenceErrors,我收到了错误。 这很好。这是有道理的。 但是,我想告诉 Dojo 构建过程所引用的内容。本质
我是一名优秀的程序员,十分优秀!