Run a function which inside the main function

3545
2
Jump to solution
02-05-2015 12:00 AM
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>

Reply
0 Kudos
1 Solution

Accepted Solutions
Regular Contributor

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

Reply
0 Kudos
2 Replies
Regular Contributor

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

Reply
0 Kudos
Occasional Contributor

Thanks, Owen.

It worked perfectly for me.

Reply
0 Kudos