dojo requirejs调用具有相同依赖关系的外部函数

jckbn6z7  于 2022-12-16  发布在  Dojo
关注(0)|答案(2)|浏览(195)

如何从函数外部调用具有相同依赖关系的函数?依赖关系不是全局的,因此被调用的函数似乎不知道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没有定义,因为它没有依赖关系。我如何添加一个依赖关系到一个外部函数,并从我的函数调用它?

pb3s4cty

pb3s4cty1#

您是否可以将FeatureLayer对象传递给getBasestationList(FeatureLayer)之类的方法,然后查看是否可以引用它?
另一种方法是声明一个全局变量,并为其指定FeatureLayer引用。
代码的问题是函数getBasestationList()似乎超出了作用域,因此需要定义一个全局变量来使用该对象。

wvt8vs2t

wvt8vs2t2#

将函数getBasestationList()放入所需函数中,如代码示例所示

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();
});

相关问题