如何从函数外部调用具有相同依赖性的函数?它似乎不是全局的,因此被调用的函数不了解FeatureLayer
require(
[
"esri/map",
"esri/layers/FeatureLayer",
"esri/dijit/PopupTemplate",
"esri/geometry/Point",
"esri/graphic",
"dojo/domReady!"
],
function(Map, FeatureLayer, PopupTemplate, Point, Graphic){
map = new Map("mapDiv", { basemap: "streets", center: [22, 65], zoom: 5 });
getBasestationList();
};
});
function getBasestationList() {
fromdatetime = fromdatetime.toLocaleString();
todatetime = todatetime.toLocaleString();
$.ajax(
{
type: "post",
url: "{% url 'get_latest_basestation_list' %}",
data:
{
"csrfmiddlewaretoken": "{{ csrf_token }}",
"fromdate": fromdatetime,
"todate": todatetime,
},
success: function (data, status)
{
featureLayer = new FeatureLayer(data, { id: 'basestations' });
map.addLayers([featureLayer]);
},
error: function (request, status, error)
{
alert(request.responseText);
}
});
};
然而,这给了我一个错误,即没有定义FeatureLayer,因为它没有那个依赖。如何向外部函数添加依赖项并从我的函数中调用它?
答案 0 :(得分:0)
您可以将FeatureLayer对象传递给getBasestationList(FeatureLayer)等方法,然后查看是否可以引用它?
另一种方法是声明一个全局变量并为其指定FeatureLayer引用。
您的代码问题是函数getBasestationList()似乎在范围之外。因此,您需要定义一个全局变量来使用该对象。
答案 1 :(得分:0)
将函数getBasestationList()放在require函数中,如代码示例
require(
[
"esri/map", "esri/layers/FeatureLayer", "esri/dijit/PopupTemplate",
"esri/geometry/Point", "esri/graphic", "dojo/domReady!"
],
function(Map, FeatureLayer, PopupTemplate, Point, Graphic){
function getBasestationList() {
fromdatetime = fromdatetime.toLocaleString();
todatetime = todatetime.toLocaleString();
$.ajax(
{
type: "post",
url: "{% url 'get_latest_basestation_list' %}"
});
};
map = new Map("mapDiv", { basemap: "streets", center: [22, 65], zoom: 5 });
getBasestationList();
});