Calling a function from outside of a request that is written inside a request in dojo

<html>
<head>    
<script>    
    require(["dojo/ready"],function(ready){

      function init(dataItem) {
          alert("inside init method")
          updateData(dataItem);
      }
      function updateData(dataItem){
       //inside this function, i am creating some breadcrumb and making each part of it a link.
       //Now that link is calling another method outerFunction()
      }
      ready(function(){
          init({
              type: "All Locations",
              id: "All_Locations"
          });
      });
   });

function outerFunction(jsonObj){
    init(jsonObj); // not working

}
</script>
</head>
<body>
   <div>
    </div>
</body>
</html>

      

Basically, I need to call init()

from outerFunction()

. Please help me with this.

I need to name init()

, defined above, from outerFunction()

. But that doesn't happen. I originally tried to keep this one outerFunction()

inside the required block. But then in this case outerFunction()

it was not called from the links palette.

So I had to move it outside. And now from outside, outerFunction()

receives a call, but init()

does not receive a call fromouterFunction()

Or anyway, I can write an externalFunction method inside require and make the same call flow.

Solution . I used dojo.subscribe () inside ready and dojo.publish () inside externalFunction (). And it worked for me.

+3


source to share


1 answer


Well this is really an area problem. You can solve it in different ways. One of them is to place outerFunction()

inside the block require()

as you mentioned.

However, if you call this function from elsewhere, you may run into a problem. For the link (you mentioned), you can use the onclick handler inside the same block require()

, for example:

on(breadcrumbLink, "click", function() {
    outerFunction({});
});

      

However, once the amount of code increases, you have a problem if you put it all in the same block require()

(because it becomes unreachable), so you can split the function init()

into a separate module that can be included for both functions.




The third solution, while it's pretty bad practice if done quite often, is to put any of them in the scope window

. If you move the function init()

to window

for example:

window.init = init;

      

Then you can access it from outerFunction()

. Another way to do it is to move it outerFunction()

inside the block require()

and add it to window

, for example:

require(/** Stuff */, function() {
    function init() { }

    function outerFunction() { }
    window.outerFunction = outerFunction;
});

      

+3


source







All Articles