Run a function which inside the main function

3911
2
Jump to solution
02-05-2015 12:00 AM
AysberqTundra
Occasional Contributor

Hi.

My question is about ArcGIS JS Api.

For some reasons I need to run a function which inside the main function from outside of main function.

How can I do it? Is there any way to do it?

<script>
    require([
      "esri/map", 
     ...
      "dojo/domReady!"
    ], function(
      Map,...
    )  {
      var map = new Map("map", {
        center: [-56.049, 38.485],
        zoom: 3,
        basemap: "streets"
      });
           

     function MyInnerFunction(){
        //body of inner funcion
     }
  });  //End of the main function

MyInnerFunction();   //Run inner function

</script>

0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Occasional Contributor III

Not sure if anyone has a better approach but as long as dojo has finished loading you can use dojo.setObject() to expose the inner function.

    <script>
      var map;


      require([
        "esri/map",
        "dojo/domReady!"
      ],
      function(Map) {
       
        function myInnerFunction(){ 
          alert("Hi from MyInnerFunction");
        }
       
        dojo.setObject('myInnerFunction', myInnerFunction);
               
        map = new Map("map", {
          basemap: "topo",
          center: [-122.45, 37.75], // longitude, latitude
          zoom: 13
        });
       
      });
     
    </script>

See this Simple Map with a button at the top left that calls a function within the require function.

View solution in original post

0 Kudos
2 Replies
OwenEarley
Occasional Contributor III

Not sure if anyone has a better approach but as long as dojo has finished loading you can use dojo.setObject() to expose the inner function.

    <script>
      var map;


      require([
        "esri/map",
        "dojo/domReady!"
      ],
      function(Map) {
       
        function myInnerFunction(){ 
          alert("Hi from MyInnerFunction");
        }
       
        dojo.setObject('myInnerFunction', myInnerFunction);
               
        map = new Map("map", {
          basemap: "topo",
          center: [-122.45, 37.75], // longitude, latitude
          zoom: 13
        });
       
      });
     
    </script>

See this Simple Map with a button at the top left that calls a function within the require function.

0 Kudos
AysberqTundra
Occasional Contributor

Thanks, Owen.

It worked perfectly for me.

0 Kudos